-
Notifications
You must be signed in to change notification settings - Fork 43
/
check.c
2326 lines (2147 loc) · 83.5 KB
/
check.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <ctype.h>
#include "gwion_util.h"
#include "gwion_ast.h"
#include "gwion_env.h"
#include "vm.h"
#include "instr.h"
#include "traverse.h"
#include "template.h"
#include "gwion.h"
#include "object.h"
#include "operator.h"
#include "import.h"
#include "parse.h"
#include "match.h"
#include "specialid.h"
#include "tmp_resolve.h"
#include "partial.h"
#include "array.h"
ANN bool check_stmt(const Env env, Stmt*);
ANN bool check_class_def(const Env env, const Class_Def class_def);
ANN static Type check_internal(const Env env, const Symbol sym, Exp* e,
const Type t) {
struct Implicit imp = {.e = e, .t = t, .loc = e->loc};
struct Op_Import opi = {.op = sym,
.lhs = e->type,
.rhs = t,
.data = (uintptr_t)&imp,
.loc = e->loc};
return op_check(env, &opi);
}
ANN bool check_implicit(const Env env, Exp* e, const Type t) {
if (e->type == t) return true;
if (isa(e->type, t)) return true;
const Symbol sym = insert_symbol("@implicit");
return !!(e->cast_to = check_internal(env, sym, e, t));
}
ANN bool check_subscripts(Env env, const Array_Sub array,
const bool is_decl) {
Exp* e = array->exp;
CHECK_B(check_exp(env, e));
m_uint depth = 0;
bool ok = true;
do {
if (is_decl) {
if(!check_implicit(env, e, env->gwion->type[et_int]))
ERR_OK_NODE(ok, e, array->exp->loc, _("invalid array declaration index type."));
}
} while (++depth && (e = e->next));
if (depth != array->depth)
ERR_B(array->exp->loc, _("invalid array access expression."));
return ok;
}
ANN static inline bool check_exp_decl_parent(const Env env,
const Var_Decl *var) {
const Value value = find_value(env->class_def->info->parent, var->tag.sym);
if (value) {
env_err(env, var->tag.loc, _("Value defined in parent class"));
defined_here(value);
return false;
}
return true;
}
ANN static m_uint get_decl_size(const Env env, const Value v) {
if(safe_tflag(env->class_def, tflag_packed) && v->type->actual_size)
return v->type->actual_size;
return v->type->size;
}
#define describe_check_decl(a, b, flag) \
ANN static inline void decl_##a(const Env env, const Value v) { \
const Nspc nspc = env->curr; \
flag; \
v->from->offset = nspc->b; \
nspc->b += get_decl_size(env, v); \
}
describe_check_decl(member, offset, v->vflag |= vflag_member);
describe_check_decl(static, class_data_size, SET_FLAG(v, static));
ANN static void var_effects(const Env env, const Type t, const Symbol sym, const loc_t loc) {
if (t->info->parent) var_effects(env, t->info->parent, sym, loc);
if(!tflag(t, tflag_ctor)) return;
const Value ctor = nspc_lookup_value0(t->nspc, sym);
if(!ctor) return; // bit sus
const Func f = ctor->d.func_ref;
const Vector v = &f->def->base->effects;
if(!v->ptr) return;
for (m_uint i = 0; i < vector_size(v); i++)
env_add_effect(env, (Symbol)vector_at(v, i), loc);
}
ANN static bool check_var(const Env env, const Var_Decl *var) {
if (env->class_def && !env->scope->depth && env->class_def->info->parent)
CHECK_B(check_exp_decl_parent(env, var));
var_effects(env, var->value->type, insert_symbol("@ctor"), var->tag.loc);
return true;
}
ANN static bool check_var_td(const Env env, const Var_Decl *var,
Type_Decl *const td) {
const Value v = var->value;
if (env->class_def) {
if (vflag(v, vflag_member)) {
decl_member(env, v);
if (env->class_def->info->tuple) tuple_info(env, v);
} else if (GET_FLAG(td, static))
decl_static(env, v);
}
return true;
}
ANN static bool check_decl(const Env env, const Exp_Decl *decl) {
const Var_Decl *vd = &decl->var.vd;
CHECK_B(check_var(env, vd));
CHECK_B(check_var_td(env, vd, decl->var.td));
if (decl->var.td->array && decl->var.td->array->exp) {
CHECK_B(check_subscripts(env, decl->var.td->array, true));
if (GET_FLAG(array_base(decl->type), abstract))
CHECK_B(check_array_instance(env, decl->var.td, decl->args));
}
valid_value(env, vd->tag.sym, vd->value);
// set_vflag(var->value, vflag_used));
return true;
}
ANN /*static inline*/ bool ensure_check(const Env env, const Type t) {
if (tflag(t, tflag_check) || !(tflag(t, tflag_cdef) || tflag(t, tflag_udef)))
return true;
if(!tflag(t, tflag_tmpl)) return true;
struct EnvSet es = {.env = env,
.data = env,
.func = (_envset_func)check_cdef,
.scope = env->scope->depth,
.flag = tflag_check};
return envset_run(&es, t);
}
ANN bool ensure_traverse(const Env env, const Type t) {
if (tflag(t, tflag_check) || !(tflag(t, tflag_cdef) || tflag(t, tflag_udef)))
return true;
if(!tflag(t, tflag_tmpl)) return true;
struct EnvSet es = {.env = env,
.data = env,
.func = (_envset_func)traverse_cdef,
.scope = env->scope->depth,
.flag = tflag_check};
return envset_run(&es, t);
}
ANN static inline bool inferable(const Env env, const Type t,
const loc_t loc) {
if (!tflag(t, tflag_infer)) return true;
ERR_B(loc, _("can't infer type."));
}
ANN Type check_exp_decl(const Env env, Exp_Decl *const decl) {
if (decl->var.td->array && decl->var.td->array->exp)
CHECK_O(check_exp(env, decl->var.td->array->exp));
if (decl->args) {
Exp* e = new_exp_unary2(env->gwion->mp, insert_symbol("new"), cpy_type_decl(env->gwion->mp, decl->var.td), decl->args, decl->var.td->tag.loc);
CHECK_O(check_exp(env, e));
decl->args = e;
e->ref = exp_self(decl);
}
if (decl->var.td->tag.sym == insert_symbol("auto")) { // should be better
CHECK_O(scan1_exp(env, exp_self(decl)));
CHECK_O(scan2_exp(env, exp_self(decl)));
}
if (!decl->type) ERR_O(decl->var.td->tag.loc, _("can't find type"));
{
CHECK_O(ensure_check(env, decl->type));
if(!inferable(env, decl->type, decl->var.td->tag.loc)) {
if(!tflag(decl->type, tflag_check) && decl->type->ref > 1 && env->class_def && !env->scope->depth)
type_remref(decl->type, env->gwion);
return NULL;
}
}
const bool global = GET_FLAG(decl->var.td, global);
const m_uint scope = !global ? env->scope->depth : env_push_global(env);
const bool ret = check_decl(env, decl);
if (global) {
env_pop(env, scope);
set_vflag(decl->var.vd.value, vflag_direct);
}
env_weight(env, 1 + is_object(env->gwion, decl->type));
return ret ? decl->var.vd.value->type : NULL;
}
ANN static bool check_collection(const Env env, Type type, Exp* e,
const loc_t loc) {
const Type common = find_common_anc(e->type, type);
if (common) return true;
if (check_implicit(env, e, type)) return true;
char fst[20 + strlen(type->name)];
sprintf(fst, "expected `{+/}%s{0}`", type->name);
gwlog_error(_("literal contains incompatible types"), fst,
env->name, loc, 0);
gwlog_hint(_("the first element determines the type"), env->name, loc);
char sec[16 + strlen(e->type->name)];
sprintf(sec, "got `{+/}%s{0}`", e->type->name);
gwlog_related(sec, env->name, e->loc);
return false;
}
ANN static inline Type prim_array_match(const Env env, Exp* e) {
const Type type = e->type;
bool err = false;
const loc_t loc = e->loc;
do
if (!check_collection(env, type, e, loc)) err = true;
while ((e = e->next));
if (!err) return array_type(env, array_base_simple(type), type->array_depth + 1, loc);
env_set_error(env, true);
return NULL;
}
ANN static Type check_prim_array(const Env env, const Array_Sub *data) {
const Array_Sub array = *data;
Exp* e = array->exp;
// if (!e)
// ERR_O(prim_pos(data), _("must provide values/expressions for array [...]"));
CHECK_O(check_exp(env, e));
env_weight(env, 1);
return array->type = prim_array_match(env, e);
}
ANN static bool check_range(const Env env, Range *range) {
if (range->start) CHECK_B(check_exp(env, range->start));
if (range->end) CHECK_B(check_exp(env, range->end));
if (range->start && range->end) {
if (!isa(range->end->type, range->start->type))
ERR_B(range->start->loc, _("range types do not match"));
}
return true;
}
ANN static Type check_prim_range(const Env env, Range **data) {
Range *range = *data;
CHECK_O(check_range(env, range));
env_weight(env, 1);
Exp* e = range->start ?: range->end;
assert(e);
const Symbol sym = insert_symbol("[:]");
struct Op_Import opi = {.op = sym,
.lhs = e->type,
.loc = e->loc,
.data = (uintptr_t)prim_exp(data)};
return op_check(env, &opi);
}
ANN static Type check_prim_dict(const Env env, Exp* *data) {
Exp* base = *data;
CHECK_O(check_exp(env, base));
const Type key = base->type;
const Type val = base->next->type;
bool err = false;
const loc_t loc = base->loc;
Exp* e = base;
env_weight(env, 1);
do {
if (!check_collection(env, key, e, loc)) err = true;
e = e->next;
if (!check_collection(env, val, e, loc)) err = true;
} while ((e = e->next));
if (!err) return dict_type(env->gwion, key, val, loc);
env_set_error(env, true); return NULL;
}
ANN bool not_from_owner_class(const Env env, const Type t, const Value v,
const loc_t loc) {
if (!v->from->owner_class || !isa(t, v->from->owner_class)) {
if(!is_class(env->gwion, v->type))
ERR_B(loc, _("'%s' from owner namespace '%s' used in '%s'."), v->name,
v->from->owner ? v->from->owner->name : "?", t->name);
}
return true;
}
ANN static inline Value get_value(const Env env, const Symbol sym) {
const Value value = nspc_lookup_value1(env->curr, sym);
if(value) {
if(value->from->owner_class && is_func(env->gwion, value->from->owner_class)) {
/*
set_tflag(value->from->owner_class, tflag_check);
set_tflag(value->from->owner_class, tflag_scan0);
set_tflag(value->from->owner_class, tflag_scan1);
set_tflag(value->from->owner_class, tflag_scan2);
set_tflag(value->from->owner_class, tflag_check);
*/
return value;
}
if (!value->from->owner_class || isa(env->class_def, value->from->owner_class))
return value;
if(env->class_def) {
if (isa(env->class_def, value->from->owner_class) || value == env->class_def->info->value)
return value;
}
}
if (env->func && env->func->def->base->values) {
DECL_O(const Value, v, = upvalues_lookup(env->func->def->base->values, sym));
if(fbflag(env->func->def->base, fbflag_lambda))
CHECK_O(not_upvalue(env, v));
return v;
}
return NULL;
}
ANN static inline bool is_value_global(const Env env, const Value v) {
if(GET_FLAG(v, global)) return true;
return from_global_nspc(env, v->from->owner);
}
ANN static Value check_non_res_value(const Env env, const Symbol *data) {
const Symbol var = *data;
const Value value = get_value(env, var);
if (env->class_def) {
if (value) {
if(value->from->owner_class)
CHECK_O(not_from_owner_class(env,
env->class_def, value, prim_pos(data)));
}
const Value v = value ?: find_value(env->class_def, var);
if (v) {
if (env->func && GET_FLAG(env->func->def->base, static) &&
vflag(v, vflag_member))
ERR_O(prim_pos(data),
_("non-static member '%s' used from static function."),
s_name(var));
}
return v;
} else if (SAFE_FLAG(env->class_def, global) ||
(env->func && GET_FLAG(env->func->def->base, global))) {
if (!value || !(is_value_global(env, value) || vflag(value, vflag_arg)))
ERR_O(prim_pos(data),
_("non-global variable '%s' used from global function/class."),
s_name(var));
} else if(env->func && fbflag(env->func->def->base, fbflag_locale)) {
if(!is_func(env->gwion, value->type) && value->from->owner && !from_global_nspc(env, value->from->owner)) // is_callable
ERR_O(prim_pos(data), _("invalid variable access from locale definition"));
}
return value;
}
ANN static Type check_dot(const Env env, const Exp_Dot *member) {
struct Op_Import opi = {.op = insert_symbol("."),
.lhs = member->base->type,
.data = (uintptr_t)member,
.loc = exp_self(member)->loc};
env_weight(env, 1);
return op_check(env, &opi);
}
ANN static bool check_upvalue(const Env env, const Exp_Primary *prim, const Value v) {
if(not_upvalue(env, v))
return true;
gwlog_error(_("value not in lambda scope"), NULL, env->name, exp_self(prim)->loc, 4242);
declared_here(v);
gwlog_hint(_("{0} try adding it to capture list"), env->name, exp_self(prim)->loc);
env_set_error(env, true);
return false;
}
ANN static Type prim_owned(const Env env, const Symbol *data) {
Exp* exp = exp_self(prim_exp(data));
const Value v = exp->d.prim.value;
if(is_class(env->gwion, v->type) && env->class_def == v->type->info->base_type) // write it better
return v->type->info->base_type;
// if(v->from->owner_class && is_func(env->gwion, v->from->owner_class))
// return v->type;
const m_str name = !GET_FLAG(v, static) ? "this" : v->from->owner_class->name;
Exp* base =
new_prim_id(env->gwion->mp, insert_symbol(name), prim_pos(data));
exp_setuse(base, 1);
exp->exp_type = ae_exp_dot;
exp->d.exp_dot.base = base;
exp->d.exp_dot.xid = *data;
return check_exp(env, exp);
}
ANN static Type prim_id_non_res(const Env env, const Symbol *data) {
const Symbol sym = *data;
const Value v = check_non_res_value(env, data);
if (!safe_vflag(v, vflag_valid)) {
const m_str name = s_name(*data);
if (!isalpha(*name) && *name != '_') {
prim_self(data)->value = env->gwion->type[et_op]->info->value;
return env->gwion->type[et_op];
}
if (env->func && fbflag(env->func->def->base, fbflag_lambda) && env->func->def->base->values) {
const Value v = upvalues_lookup(env->func->def->base->values, sym);
if(v) {
CHECK_O(check_upvalue(env, prim_self(data), v));
return v->type;
}
}
m_str str = NULL;
gw_asprintf(env->gwion->mp, &str, "Invalid variable {R}%s{0}\n", name);
gwlog_error(str, _("not legit at this point."),
env->name, prim_pos(data), 0);
free_mstr(env->gwion->mp, str);
did_you_mean_nspc(v ? v->from->owner : env->curr, s_name(sym));
env_set_error(env, true);
return NULL;
}
prim_self(data)->value = v;
if (v->from->owner_class) return prim_owned(env, data);
if (GET_FLAG(v, const)) exp_setmeta(prim_exp(data), 1);
if (env->func && strcmp(env->func->name, "in spork")) {
// if(vflag(v, vflag_fglobal) /*&& !vflag(v, vflag_builtin) */&& !is_func(env->gwion, v->type)) {
if((GET_FLAG(v, global) || vflag(v, vflag_fglobal)) && !vflag(v, vflag_builtin) && !is_func(env->gwion, v->type)) {
if(!env->func->_wait)
env->func->_wait = new_mp_vector(env->gwion->mp, Value, 0);
if (!v->used_by) {
v->used_by = new_mp_vector(env->gwion->mp, Func, 1);
mp_vector_set(v->used_by, Func, 0, env->func);
mp_vector_add(env->gwion->mp, &env->func->_wait, Value, v);
} else {
bool found = false;
for(uint32_t i = 0; i < v->used_by->len; i++) {
const Func f = *mp_vector_at(v->used_by, Func, i);
if(f == env->func) {
found = true;
break;
}
}
if(!found) {
mp_vector_add(env->gwion->mp, &v->used_by, Func, env->func);
mp_vector_add(env->gwion->mp, &env->func->_wait, Value, v);
}
}
}
if (!GET_FLAG(v, const) && v->from->owner)
unset_fflag(env->func, fflag_pure);
if (fbflag(env->func->def->base, fbflag_lambda))
CHECK_O(check_upvalue(env, prim_self(data), v));
}
// set_vflag(v->vflag, vflag_used);
return v->type;
}
ANN Type check_prim_str(const Env env, const struct AstString *data) {
if (!prim_self(data)->value)
prim_self(data)->value = global_string(env, data->data, prim_pos(data));
exp_setmeta(prim_exp(data), true);
return env->gwion->type[et_string]; // prim->value
}
ANN static Type check_prim_id(const Env env, const Symbol *data) {
struct SpecialId_ *spid = specialid_get(env->gwion, *data);
if (spid) return specialid_type(env, spid, prim_self(data));
return prim_id_non_res(env, data);
}
ANN static Type check_prim_perform(const Env env, const Symbol *data) {
if(*data) env_add_effect(env, *data, prim_pos(data));
else if(!env->scope->in_try)
ERR_O(prim_pos(data), "`rethrow` outside try/handle statement");
env_weight(env, 1);
return env->gwion->type[et_void];
}
ANN static Type check_prim_interp(const Env env, Exp* *exp) {
CHECK_O(check_exp(env, *exp));
env_weight(env, 1);
return env->gwion->type[et_string];
}
ANN static Type check_prim_hack(const Env env, Exp* *data) {
if (env->func) unset_fflag(env->func, fflag_pure);
CHECK_O(check_prim_interp(env, data));
env_weight(env, 1);
return env->gwion->type[et_gack];
}
ANN static Type check_prim_locale(const Env env, const Symbol *data NUSED) {
exp_setmeta(prim_exp(data), true);
return env->gwion->type[et_float];
}
#define describe_prim_xxx(name, type) \
ANN static Type check_prim_##name(const Env env NUSED, \
const union prim_data *data NUSED) { \
return type; \
}
describe_prim_xxx(num, env->gwion->type[et_int]);
describe_prim_xxx(char, env->gwion->type[et_char]);
describe_prim_xxx(float, env->gwion->type[et_float]);
describe_prim_xxx(nil, env->gwion->type[et_void]);
DECL_PRIM_FUNC(check, Type, Env);
ANN static Type check_prim(const Env env, Exp_Primary *prim) {
return exp_self(prim)->type = check_prim_func[prim->prim_type](env, &prim->d);
}
ANN Type check_array_access(const Env env, const Array_Sub array) {
const Symbol sym = insert_symbol("[]");
struct Op_Import opi = {.op = sym,
.lhs = array->exp->type,
.rhs = array->type,
.loc = array->exp->loc,
.data = (uintptr_t)array};
return op_check(env, &opi);
}
static ANN Type check_exp_array(const Env env, const Exp_Array *array) {
CHECK_O((array->array->type = check_exp(env, array->base)));
CHECK_O(check_subscripts(env, array->array, false));
if(exp_getmeta(array->base)) exp_setmeta(exp_self(array), true);
return check_array_access(env, array->array);
}
static ANN Type check_exp_slice(const Env env, const Exp_Slice *range) {
CHECK_O(check_exp(env, range->base));
CHECK_O(check_range(env, range->range));
env_weight(env, 1);
const Symbol sym = insert_symbol("[:]");
Exp* e = range->range->start ?: range->range->end;
assert(e);
struct Op_Import opi = {.op = sym,
.lhs = e->type,
.rhs = range->base->type,
.loc = e->loc,
.data = (uintptr_t)exp_self(range)};
return op_check(env, &opi);
}
// get the type of the function
// without the mangling
ANN static inline Type tmplarg_list_base_func(const Type type) {
const Nspc owner = type->info->value->from->owner;
const Symbol xid = type->info->func->def->base->tag.sym;
return nspc_lookup_type0(owner, xid);
}
ANN static inline Type tmplarg_list_base(const Gwion gwion, const Type type) {
return !(is_func(gwion, type)) ? type : tmplarg_list_base_func(type);
}
ANN static Type_Decl* mk_td(const Env env, Type_Decl *td,
const Type type, const loc_t loc) {
const Type base = tmplarg_list_base(env->gwion, type);
const Type t = !td->array ?
base : array_type(env, base, td->array->depth, loc);
return type2td(env->gwion, t, loc);
}
ANN static Type tmplarg_match(const Env env, const Symbol xid, const Symbol tgt, const Type t) {
if (xid == tgt) return t;
// Tmpl *tmpl = get_tmpl(t);//tflag(t, tflag_cdef) ? t->info->cdef->base.tmpl
Tmpl *tmpl = tflag(t, tflag_cdef) ? t->info->cdef->base.tmpl
: tflag(t, tflag_udef) ? t->info->udef->tmpl
: NULL;
if (!tmpl) return NULL;
const uint32_t len = mp_vector_len(tmpl->list);
for(uint32_t i = 0; i < len; i++) {
Specialized *spec = mp_vector_at(tmpl->list, Specialized, i);
const Type base = nspc_lookup_type1(t->nspc, spec->tag.sym);
const Type t = tmplarg_match(env, xid, spec->tag.sym, base);
if(t) return t;
}
return NULL;
}
ANN2(1, 2)
static Func find_func_match_actual(const Env env, const Func f, Exp* exp,
const bool implicit, const bool specific) {
Func func = f;
do {
Exp* e = exp;
Arg_List args = func->def->base->args;
m_uint i = 0;
const m_uint args_len = mp_vector_len(args);
while (e) {
e->cast_to = NULL;
if (!e->type) // investigate
return NULL;
Arg *arg = i < args_len ? mp_vector_at(args, Arg, i++) : NULL;
if (!arg) {
CHECK_O(func->next);
return find_func_match_actual(env, func->next, exp, implicit,
specific);
}
if (tflag(e->type, tflag_ref) && isa(e->type, arg->type)) {
if(!e->cast_to)e->cast_to = arg->type;
}
if (arg->type == env->gwion->type[et_auto]) {
const Type owner = func->value_ref->from->owner_class;
if (owner) CHECK_O(template_push(env, owner));
arg->type = known_type(env, arg->var.td);
if (owner) nspc_pop_type(env->gwion->mp, env->curr);
CHECK_O(arg->type);
}
if (!func_match_inner(env, e, arg->type, implicit, specific)) break;
e = e->next;
}
if (!e && args_len == i) return func;
} while ((func = func->next));
return NULL;
}
ANN static Type check_exp_call(const Env env, Exp_Call *exp);
ANN static Func call2ufcs(const Env env, Exp_Call *call, const Value v) {
Exp* this = call->func->d.exp_dot.base;
this->next = call->args;
call->args = this;
call->func->type = v->type;
call->func->d.prim.value = v;
call->func->d.prim.d.var = call->func->d.exp_dot.xid;
call->func->exp_type = ae_exp_primary;
call->func->d.prim.prim_type = ae_prim_id;
CHECK_O(check_exp_call(env, call));
return call->func->type->info->func;
}
ANN static Func ufcs(const Env env, const Func up, Exp_Call *const call) {
const Value v = nspc_lookup_value1(env->curr, up->def->base->tag.sym);
if (v && is_func(env->gwion, v->type) && !v->from->owner_class) // is_callable
return call2ufcs(env, call, v);
return NULL;
}
ANN Func find_func_match(const Env env, const Func up, Exp_Call *const call) {
Func func;
Exp* exp = call->args;
Exp* args =
(exp && !isa(exp->type, env->gwion->type[et_void])) ? exp : NULL;
if ((func = find_func_match_actual(env, up, args, false, true)) ||
(func = find_func_match_actual(env, up, args, true, true)) ||
(func = find_func_match_actual(env, up, args, false, true)) ||
(func = find_func_match_actual(env, up, args, true, false)))
return func;
return call->func->exp_type == ae_exp_dot && up->value_ref->from->owner_class
? ufcs(env, up, call)
: NULL;
}
ANN bool check_traverse_fdef(const Env env, const Func_Def fdef) {
struct Vector_ v = {};
const m_uint scope = env->scope->depth;
env->scope->depth = 0;
vector_init(&v);
const Vector w = (Vector)&env->curr->info->value->ptr;
m_uint i = vector_size(w);
while (i-- > 1) vector_add(&v, vector_at(w, i));
VLEN(w) = 1;
const bool ret = traverse_func_def(env, fdef);
for (m_uint i = vector_size(&v) + 1; --i;)
vector_add(w, vector_at(&v, i - 1));
vector_release(&v);
env->scope->depth = scope;
return ret;
}
ANN static bool check_func_args(const Env env, Arg_List args) {
bool ok = true;
for(uint32_t i = 0; i < args->len; i++) {
Arg *arg = mp_vector_at(args, Arg, i);
const Var_Decl *decl = &arg->var.vd;
const Value v = decl->value;
if(decl->tag.sym) {
if(!can_define(env, decl->tag.sym, decl->tag.loc)) {
POISON(ok, env);
continue;
}
valid_value(env, decl->tag.sym, v);
}
}
return ok;
}
ANN static inline Exp* next_arg_exp(const Exp *e) {
Exp* next = e->next;
if (next) gw_err(", ");
return next;
}
ANN static void print_current_args(Exp* e) {
do gw_err(" {G}%s{0}", e->type ? e->type->name : "<Unknown>");
while ((e = next_arg_exp(e)));
gw_err("\n");
}
ANN2(1, 2)
static void function_alternative(const Env env, const Type t, Exp* args,
const loc_t loc) {
const bool is_closure = !isa(t, env->gwion->type[et_closure]);
Func f = is_closure
? t->info->func
: closure_def(t)->base->func;
if(!f) return;
gwlog_error("Argument type mismatch", "call site",
env->name, loc, 0);
// TODO: hint valid alternatives
do print_signature(env->gwion, f);
while ((f = f->next));
gw_err(_("and not:\n "));
if (args)
print_current_args(args);
else
gw_err(_(" {G}void{0}\n"));
env_set_error(env, true);
}
ANN static Func get_template_func(const Env env, Exp_Call *const func,
const Value v) {
const Func f = find_template_match(env, v, func);
if (f) {
Tmpl *tmpl = new_tmpl_call(env->gwion->mp, func->tmpl->call);
tmpl->list = v->d.func_ref
? v->d.func_ref->def->base->tmpl->list
: func->func->type->info->func->def->base->tmpl->list;
((Exp_Call *)func)->tmpl = tmpl;
func->func->type = f->value_ref->type;
return f;
}
((Exp_Call *)func)->tmpl = NULL;
assert(exp_self(func));
ERR_O(exp_self(func)->loc,
_("function is a template for which automatic type inference is not fully implemented"));
}
ANN static Func predefined_func(const Env env, const Value v, Exp_Call *exp,
const Tmpl *tm) {
Tmpl tmpl = {.call = tm->call};
exp->tmpl = &tmpl;
DECL_O(const Func, func, = get_template_func(env, exp, v));
return v->d.func_ref = func;
}
ANN static Type check_predefined(const Env env, Exp_Call *exp, const Value v,
const Tmpl *tm, const Func_Def fdef) {
DECL_O(const Func, func,
= v->d.func_ref ?: predefined_func(env, v, exp, tm));
if (!fdef->base->ret_type) { // template fptr
struct EnvSet es = {.env = env,
.data = env,
.func = (_envset_func)check_cdef,
.scope = env->scope->depth,
.flag = tflag_check};
CHECK_O(envset_pushv(&es, v));
func->def->base->fbflag |= fbflag_internal;
const bool ret = check_traverse_fdef(env, func->def);
envset_popv(&es, v);
CHECK_O(ret);
}
exp->func->type = func->value_ref->type;
return func->def->base->ret_type;
}
ANN static TmplArg_List check_template_args(const Env env, Exp_Call *exp,
const Tmpl *tm, const Func_Def fdef) {
Specialized_List sl = tm->list;
const bool spread = is_spread_tmpl(fdef->base->tmpl);
const uint32_t len = sl->len - spread;
TmplArg_List tl = new_mp_vector(env->gwion->mp, TmplArg, len);
m_uint args_number = 0;
// infer template types from args
// should not work with const generic
bool ok = true;
if(exp->other) {
for(uint32_t i = 0; i < len; i++) {
Specialized *spec = mp_vector_at(sl, Specialized, i);
if (tmplarg_match(env, spec->tag.sym, fdef->base->td->tag.sym, fdef->base->ret_type)) {
if(!check_exp(env, exp->other)) {
POISON_NODE(ok, env, exp->other);
continue;
}
if(!is_func(env->gwion, exp->other->type)) {
TmplArg targ = {
.type = tmplarg_td,
.d = { .td = type2td(env->gwion, exp->other->type, fdef->base->td->tag.loc) }
};
mp_vector_set(tl, TmplArg, 0, targ);
} else {
Func func = exp->other->type->info->func;
do {
if(mp_vector_len(func->def->base->args) == 1) {
Arg *arg = mp_vector_at(func->def->base->args, Arg, 0);
TmplArg targ = {
.type = tmplarg_td,
.d = { .td = cpy_type_decl(env->gwion->mp, arg->var.td) }
};
mp_vector_set(tl, TmplArg, 0, targ);
break;
}
} while((func = func->next));
}
++args_number;
break;
}
}
}
CHECK_B(ok);
for(uint32_t i = 0; i < len; i++) {
Specialized *spec = mp_vector_at(sl, Specialized, i);
Arg_List args = fdef->base->args;
const uint32_t args_len = mp_vector_len(args);
Exp* template_arg = exp->args;
uint32_t count = 0;
while (count < args_len && template_arg) {
Arg *arg = mp_vector_at(args, Arg, count);
if (tmplarg_match(env, spec->tag.sym, arg->var.td->tag.sym, template_arg->type)) {
TmplArg targ = {
.type = tmplarg_td,
.d = { .td = mk_td(env, arg->var.td, template_arg->type, fdef->base->tag.loc) }
};
mp_vector_set(tl, TmplArg, args_number, targ);
++args_number;
break;
}
count++;
template_arg = template_arg->next;
}
}
if (args_number < len) //TODO: free tmplarg_list
ERR_O(exp->func->loc, _("not able to infer types for template call."));
if(spread) {
Exp* e = exp->args;
const uint32_t len = fdef->base->args ? fdef->base->args->len : 0;
// if(fdef->base->args) {
for(uint32_t i = 0; i < len && e; i++) e = e->next;
while(e) {
TmplArg targ = {
.type = tmplarg_td,
.d = { .td = type2td(env->gwion, e->type, e->loc) }
};
mp_vector_add(env->gwion->mp, &tl, TmplArg, targ);
e = e->next;
}
// }
}
return tl;
}
ANN static Type check_exp_call_template(const Env env, Exp_Call *exp) {
/*const */Type t = exp->func->type;
if(isa(t, env->gwion->type[et_closure])) {
const Func_Def fdef = closure_def(t);
t = fdef->base->func->value_ref->type;
}
DECL_O(const Value, value, = type_value(env->gwion, t));
const Func_Def fdef =
value->d.func_ref ? value->d.func_ref->def : t->info->func->def;
Tmpl *tm = fdef->base->tmpl;
if (tm->call) return check_predefined(env, exp, value, tm, fdef);
DECL_O(const TmplArg_List, tl, = check_template_args(env, exp, tm, fdef));
Tmpl tmpl = {.call = tl};
((Exp_Call *)exp)->tmpl = &tmpl;
DECL_O(const Func, func, = get_template_func(env, exp, value));
return func->def->base->ret_type != env->gwion->type[et_auto] ?
func->def->base->ret_type : exp->func->d.exp_dot.base->type;
}
ANN Type upvalue_type(const Env env, Capture *cap);
ANN static bool lambda_args_ref(const Env env, Exp_Call *const call) {
Exp* e = call->args;
CHECK_B(check_exp(env, e));
do if(tflag(e->type, tflag_ref) && !safe_tflag(exp_self(e)->cast_to, tflag_ref))
exp_setvar(e, true);
while((e = e->next));
return true;
}
ANN2(1) static bool lambda_append_args(const Env env, Exp_Call *const call, Exp* add) {
if(!add) return false;
if (call->args) {
Exp* e = call->args;
while(e->next) e = e->next;
e->next = add;
} else call->args = add;
return traverse_exp(env, add);
}
ANN static Exp* check_lambda_captures(const Env env, const Func_Def fdef) {
if(!fdef->base->args)
fdef->base->args = new_mp_vector(env->gwion->mp, Arg, 0);
Exp* args = NULL;
Exp* tmp;
for(uint32_t i = 0; i < fdef->captures->len; i++) {
Capture *const cap = mp_vector_at(fdef->captures, Capture, i);
const Type t = upvalue_type(env, cap);
if(!t) {
if(args) free_exp(env->gwion->mp, args);
return NULL;
}
Arg arg = { .var = MK_VAR(
type2td(env->gwion, t, cap->var.tag.loc),
(Var_Decl){ .tag = MK_TAG(cap->var.tag.sym, cap->var.tag.loc) }
)};
mp_vector_add(env->gwion->mp, &fdef->base->args, Arg, arg);
Exp* exp = new_prim_id(env->gwion->mp, cap->var.tag.sym, cap->var.tag.loc);
if(args) tmp = tmp->next = exp;
else args = tmp = exp;
}
free_mp_vector(env->gwion->mp, Capture, fdef->captures);
fdef->captures = NULL;
return args;
}
ANN static Type check_lambda_call(const Env env, Exp_Call *const exp) {
const Func_Def fdef = exp->func->d.exp_lambda.def;
const bool captures = !!fdef->captures;
if (exp->args) CHECK_O(lambda_args_ref(env, exp));
Exp* _args = !captures ? NULL : check_lambda_captures(env, fdef);
if(captures) CHECK_O(lambda_append_args(env, exp, _args));
Exp_Lambda *l = &exp->func->d.exp_lambda;
Arg_List args = l->def->base->args;
Exp* e = exp->args;
for(uint32_t i = 0; i < (args ? args->len : 0); i++) {
if(!e)
ERR_O(exp_self(exp)->loc, _("argument number does not match for lambda"));
Arg *arg = mp_vector_at(args, Arg, i);
arg->type = e->type;
if(is_class(env->gwion, arg->type))
type_addref(arg->type);
e = e->next;
}
if(e)
ERR_O(exp_self(exp)->loc, _("argument number does not match for lambda"));
Upvalues upvalues = { .values = env->curr->info->value};
if(env->func && env->func->def->base->values)
upvalues.parent = env->func->def->base->values;
l->def->base->values = &upvalues;
const bool ret = traverse_func_def(env, l->def);
if (l->def->base->func) {
free_scope(env->gwion->mp, env->curr->info->value);
env->curr->info->value = l->def->base->values->values;
if (env->class_def) set_vflag(l->def->base->func->value_ref, vflag_member);
exp->func->type = l->def->base->func->value_ref->type;
if (!l->def->base->ret_type)
l->def->base->ret_type = env->gwion->type[et_void];
}
return ret ? l->def->base->ret_type : NULL;
}
ANN bool func_check(const Env env, Exp_Call *const exp, bool *mod) {
exp->func->is_call = true;
CHECK_B(check_exp(env, exp->func));
if (exp->func->exp_type == ae_exp_decl)
ERR_B(exp->func->loc, _("Can't call late function pointer at declaration "
"site. did you meant to use `:=>`?"));
const Type t = actual_type(env->gwion, exp->func->type);
if (is_func(env->gwion, t) && exp->func->exp_type == ae_exp_dot && // is_callable
!t->info->value->from->owner_class) {
if (exp->args) CHECK_B(check_exp(env, exp->args));
return !!call2ufcs(env, exp, t->info->func->value_ref);
}
Exp* e = exp_self(exp);
struct Op_Import opi = {.op = insert_symbol("@func_check"),
.rhs = t,
.loc = e->loc,
.data = (uintptr_t)e};
if(op_get(env, &opi))
CHECK_B(op_check(env, &opi));
if (e->exp_type != ae_exp_call) {
*mod = true;
return true;
}
return e->type != env->gwion->type[et_error];
}
ANN void call_add_effect(const Env env, const Func func, const loc_t loc) {
if (func != env->func && func->def->base->effects.ptr) {
const Vector v = &func->def->base->effects;
for (m_uint i = 0; i < vector_size(v); i++)
env_add_effect(env, (Symbol)vector_at(v, i), loc);
}
}
ANN Type call_type(const Env env, Exp_Call *const exp) {
const Type t = exp->func->type;
if (is_func(env->gwion, t)) return t;
if(isa(exp->func->type, env->gwion->type[et_closure]))
return closure_def(t)->base->func->value_ref->type;
if(is_class(env->gwion, t) && tflag(t->info->base_type, tflag_struct)) {
const Value v = nspc_lookup_value0(t->info->base_type->nspc, insert_symbol("new"));
if(v) return exp->func->type = v->type;
}
struct Op_Import opi = {.op = insert_symbol("call_type"),
.rhs = actual_type(env->gwion, exp->func->type),