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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
|
/* SPDX-License-Identifier: copyleft-next-0.3.1 */
/* Copyright 2024 Kim Kuparinen < kimi.h.kuparinen@gmail.com > */
#include <fwd/compiler.h>
#include <fwd/analyze.h>
#include <fwd/rewrite.h>
#include <fwd/mod.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include <dlfcn.h>
enum state_flags {
STATE_PROC_REQ_FRAME = (1 << 0)
};
struct state {
enum state_flags flags;
};
static int analyze(struct state *state, struct scope *scope, struct ast *node);
static int analyze_known_block(struct state *state, struct scope *scope, struct ast *node);
static int analyze_list(struct state *state, struct scope *scope,
struct ast *nodes);
static int analyze_type(struct state *state, struct scope *scope,
struct type *type);
static int analyze_type_list(struct state *state, struct scope *scope,
struct type *types);
static int analyze_proc(struct state *state, struct scope *scope,
struct ast *node)
{
(void)state;
(void)scope;
struct scope *proc_scope = create_scope();
if (!proc_scope) {
internal_error("failed allocating proc scope");
return -1;
}
proc_scope->flags |= SCOPE_PROC;
scope_add_scope(scope, proc_scope);
struct state proc_state = {};
if (analyze_list(&proc_state, proc_scope, proc_params(node)))
return -1;
struct type *proc_type = tgen_func_ptr(NULL, node->loc);
if (!proc_type) {
internal_error("failed allocating proc type");
return -1;
}
size_t group = 0;
foreach_node(param, proc_params(node)) {
if (param->ol == NULL && param->or == NULL)
group++; /* no opt group */
if (param->ol == NULL && param->or)
group++; /* start of new group */
/* otherwise same or ending group, don't increment */
param->t->group = group;
type_append(&proc_type->t0, clone_type(param->t));
}
node->t = proc_type;
if (!proc_body(node))
return 0;
if (analyze_known_block(&proc_state, proc_scope, proc_body(node)))
return -1;
if (proc_state.flags & STATE_PROC_REQ_FRAME)
ast_set_flags(node, AST_REQ_FRAME);
return 0;
}
static int analyze_unop(struct state *state, struct scope *scope,
struct ast *node)
{
/** @todo check expr is some primitive type */
struct ast *expr = unop_expr(node);
if (analyze(state, scope, expr))
return -1;
node->t = expr->t;
return 0;
}
static int analyze_binop(struct state *state, struct scope *scope,
struct ast *node)
{
struct ast *lhs = binop_left(node);
struct ast *rhs = binop_right(node);
if (analyze(state, scope, lhs))
return -1;
if (analyze(state, scope, rhs))
return -1;
if (!types_match(lhs->t, rhs->t)) {
type_mismatch(scope, node, lhs->t, rhs->t);
return -1;
}
/** @todo check type is some primitive */
node->t = lhs->t;
return 0;
}
static int analyze_comparison(struct state *state, struct scope *scope,
struct ast *node)
{
struct ast *lhs = comparison_left(node);
struct ast *rhs = comparison_right(node);
if (analyze(state, scope, lhs))
return -1;
if (analyze(state, scope, rhs))
return -1;
if (!types_match(lhs->t, rhs->t)) {
type_mismatch(scope, node, lhs->t, rhs->t);
return -1;
}
/** @todo check type is some primitive */
char *tf = strdup("bool");
if (!tf) {
internal_error("failed allocating comparison bool str");
return -1;
}
node->t = tgen_id(tf, node->loc);
if (!node->t) {
internal_error("failed allocating comparison bool type");
free(tf);
return -1;
}
return 0;
}
static int analyze_known_block(struct state *state, struct scope *scope, struct ast *node)
{
assert(node && node->k == AST_BLOCK);
node->scope = scope;
node->t = tgen_void(node->loc);
if (analyze_list(state, scope, block_body(node)))
return -1;
return 0;
}
static int analyze_block(struct state *state, struct scope *scope,
struct ast *node)
{
struct scope *block_scope = create_scope();
if (!block_scope) {
internal_error("failed to allocate block scope");
return -1;
}
scope_add_scope(scope, block_scope);
return analyze_known_block(state, scope, node);
}
static int analyze_var(struct state *state, struct scope *scope,
struct ast *node)
{
if (analyze_type(state, scope, var_type(node)))
return -1;
node->t = var_type(node);
return scope_add_symbol(scope, node);
}
static int analyze_let(struct state *state, struct scope *scope,
struct ast *node)
{
if (analyze(state, scope, let_var(node)))
return -1;
if (analyze(state, scope, let_expr(node)))
return -1;
struct type *l = (let_var(node))->t;
struct type *r = (let_expr(node))->t;
if (!types_match(l, r)) {
type_mismatch(scope, node, l, r);
return -1;
}
node->t = l;
return 0;
}
static int analyze_init(struct state *state, struct scope *scope,
struct ast *node)
{
if (analyze_type_list(state, scope, init_args(node)))
return -1;
if (analyze(state, scope, init_body(node)))
return -1;
internal_error("init unimplemented");
return -1;
}
static int deduce_closure_types(struct scope *scope, struct ast *node, struct type *type)
{
/* a bit of checking duplication here, hmm */
if (node->k != AST_CLOSURE)
return 0;
if (type->k != TYPE_CLOSURE && type->k != TYPE_PURE_CLOSURE) {
semantic_error(scope, node, "surprise closure");
return -1;
}
struct ast *param = closure_bindings(node);
struct type *t = tclosure_args(type);
if (ast_list_len(param) != type_list_len(t)) {
semantic_error(scope, node, "expected %zu closure parameters, got %zu",
type_list_len(t), ast_list_len(param));
return -1;
}
for (; param && t; param = param->n, t = t->n) {
if (var_type(param))
continue;
var_type(param) = t;
}
return 0;
}
static int analyze_call(struct state *state, struct scope *scope,
struct ast *node)
{
struct ast *expr = call_expr(node);
if (analyze(state, scope, expr))
return -1;
if (!is_callable(expr->t)) {
semantic_error(scope, node, "expected callable expression");
return -1;
}
struct type *types = callable_types(expr->t);
struct ast *args = call_args(node);
size_t expected = type_list_len(types);
size_t got = ast_list_len(args);
if (expected != got) {
semantic_error(scope, node, "expected %d params, got %d",
expected, got);
return -1;
}
struct type *type = types;
struct ast *arg = args;
for (; arg && type; type = type->n, arg = arg->n) {
if (deduce_closure_types(scope, arg, type))
return -1;
if (analyze(state, scope, arg))
return -1;
if (arg->k == AST_CLOSURE)
state->flags |= STATE_PROC_REQ_FRAME;
if (!types_match(type, arg->t)) {
type_mismatch(scope, arg, type, arg->t);
return -1;
}
/* clone group info */
arg->t->group = type->group;
}
node->t = tgen_void(node->loc);
if (!node->t) {
internal_error("failed allocating void type for call");
return -1;
}
return 0;
}
static int analyze_ref(struct state *state, struct scope *scope,
struct ast *node)
{
struct ast *expr = ref_base(node);
if (analyze(state, scope, expr))
return -1;
if (!is_lvalue(expr)) {
semantic_error(node->scope, node, "trying to reference rvalue");
return -1;
}
struct type *ref = tgen_ref(expr->t, node->loc);
if (!ref) {
internal_error("failed allocating ref type");
return -1;
}
node->t = ref;
return 0;
}
static int analyze_deref(struct state *state, struct scope *scope,
struct ast *node)
{
struct ast *expr = deref_base(node);
if (analyze(state, scope, expr))
return -1;
if (expr->t->k == TYPE_PTR) {
semantic_error(node->scope, node,
"deref of raw ptr not allowed");
semantic_info(node->scope, node,
"use a nil check to convert to ref");
return -1;
}
if (expr->t->k != TYPE_REF) {
semantic_error(node->scope, node,
"deref of something not a reference");
return -1;
}
node->t = tref_base(expr->t);
return 0;
}
static int analyze_id(struct state *state, struct scope *scope,
struct ast *node)
{
struct ast *found = file_scope_find_symbol(scope, id_str(node));
if (!found) {
semantic_error(scope, node, "no symbol named \"%s\"",
id_str(node));
return -1;
}
/* kind of hacky, functions are given their type before they've been
* analyzed fully, this enables recursion */
if (!found->t && !ast_flags(found, AST_FLAG_ANALYZED)) {
assert(found->k == AST_PROC_DEF);
/* a proc def will use its own state and scope, but pass these
* on in case the analysis wants to print errors or something
*/
if (analyze(state, scope, found))
return -1;
}
node->t = found->t;
return 0;
}
static int analyze_closure(struct state *state, struct scope *scope,
struct ast *node)
{
struct scope *closure_scope = create_scope();
if (!closure_scope) {
internal_error("failed allocating closure scope");
return -1;
}
node->scope = closure_scope;
closure_scope->flags |= SCOPE_PROC;
scope_add_scope(scope, closure_scope);
if (analyze_list(state, closure_scope, closure_bindings(node)))
return -1;
if (analyze_known_block(state, closure_scope, closure_body(node)))
return -1;
struct type *callable = NULL;
if (ast_flags(node, AST_FLAG_NOMOVES))
callable = tgen_pure_closure(NULL, node->loc);
else
callable = tgen_closure(NULL, node->loc);
if (!callable) {
internal_error("failed allocating closure type");
return -1;
}
foreach_node(binding, closure_bindings(node)) {
type_append(&callable->t0, clone_type(binding->t));
}
node->t = callable;
return 0;
}
static int analyze_int(struct state *state, struct scope *scope,
struct ast *node)
{
(void)state;
(void)scope;
/* start with largest possible and work down */
node->t = tgen_type(TYPE_I64, NULL, NULL, node->loc);
if (!node->t) {
internal_error("failed allocating constant int type");
return -1;
}
return 0;
}
static int analyze_str(struct state *state, struct scope *scope,
struct ast *node)
{
(void)state;
(void)scope;
/** @todo do this properly, very hacky, bad bad bad */
char *i = strdup("char");
if (!i) {
internal_error("failed allocating constant char type string");
return -1;
}
struct type *ch = tgen_id(i, node->loc);
if (!ch) {
internal_error("failed allocating constant char type");
free(i);
return -1;
}
struct type *str = tgen_ptr(ch, node->loc);
if (!str) {
internal_error("failed allocating constant str type");
return -1;
}
node->t = str;
return 0;
}
static int analyze_template(struct state *state, struct scope *scope, struct ast *node)
{
struct scope *template_scope = create_scope();
scope_add_scope(scope, template_scope);
/* enough for vec example since traits are as of yet unimplemented */
node->t = tgen_void(node->loc);
return 0;
}
static int analyze_instance(struct state *state, struct scope *scope, struct ast *node)
{
(void)state;
struct ast *id = instance_templ(node);
struct ast *template = file_scope_find_template(scope, id_str(id));
if (!template) {
semantic_error(scope, node, "no such template: %s\n", id_str(id));
return -1;
}
/** @todo check that type implements trait */
struct ast *params = template_type_params(template);
struct type *args = instance_type_args(node);
if (ast_list_len(params) != type_list_len(args)) {
semantic_error(scope, node, "expected %zu types, got %zu\n",
ast_list_len(params), type_list_len(args));
return -1;
}
/* make a working copy that we modify */
struct ast *copy = clone_ast(template);
if (!copy) {
internal_error("failed copying template body");
return -1;
}
if (rewrite_holes(copy, instance_id(node)))
return -1;
/* replace type args */
struct ast *p = params;
struct type *a = args;
for (; p && a; p = p->n, a = a->n) {
if (rewrite_types(copy, var_id(p), tid_str(a)))
return -1;
}
printf("// --- instance %s ---\n", instance_id(node));
ast_dump(0, copy);
/* analyze newly initialized things (might not work for supertemplates?) */
if (analyze_root(scope, template_body(copy)))
return -1;
node->t = tgen_void(node->loc);
return 0;
}
static int analyze_struct(struct state *state, struct scope *scope, struct ast *node)
{
struct scope *struct_scope = create_scope();
scope_add_scope(scope, struct_scope);
node->scope = struct_scope;
if (analyze_list(state, struct_scope, struct_body(node)))
return -1;
node->t = tgen_void(node->loc);
return 0;
}
static int analyze_construct(struct state *state, struct scope *scope, struct ast *node)
{
struct ast *def = file_scope_find_type(scope, construct_id(node));
if (!def) {
semantic_error(scope, node, "no such type");
return -1;
}
if (def->k != AST_STRUCT_DEF) {
semantic_error(scope, node, "type is not a struct");
return -1;
}
struct ast *params = struct_body(def);
struct ast *args = construct_members(node);
if (ast_list_len(params) != ast_list_len(args)) {
semantic_error(scope, node, "expected %zu args, got %zu\n",
ast_list_len(params), ast_list_len(args));
return -1;
}
foreach_node(arg, args) {
struct ast *exists = scope_find_symbol(def->scope, construction_id(arg));
if (!exists) {
semantic_error(scope, arg, "no member %s in %s\n",
construction_id(arg), construct_id(node));
return -1;
}
struct ast *expr = construction_expr(arg);
if (analyze(state, scope, expr))
return -1;
if (!types_match(exists->t, expr->t)) {
type_mismatch(scope, arg, exists->t, expr->t);
return -1;
}
/** @todo check for duplicates, preferably not in O(n^2) or with
* a memory allocation? */
}
node->t = tgen_id(strdup(construct_id(node)), node->loc);
return 0;
}
static int analyze_if(struct state *state, struct scope *scope,
struct ast *node)
{
if (analyze(state, scope, if_cond(node)))
return -1;
if (analyze(state, scope, if_body(node)))
return -1;
if (analyze(state, scope, if_else(node)))
return -1;
node->t = tgen_void(node->loc);
if (!node->t) {
internal_error("failed allocating 'if' void type");
return -1;
}
return 0;
}
static int analyze_nil(struct state *state, struct scope *scope, struct ast *node)
{
(void)state;
(void)scope;
node->t = tgen_nil(node->loc);
return 0;
}
static bool castable_type(struct type *type)
{
switch (type->k) {
case TYPE_PTR:
case TYPE_FUNC_PTR:
case TYPE_I8:
case TYPE_I16:
case TYPE_I32:
case TYPE_I64:
case TYPE_U8:
case TYPE_U16:
case TYPE_U32:
case TYPE_U64:
return true;
default:
return false;
}
return false;
}
static int analyze_as(struct state *state, struct scope *scope, struct ast *node)
{
if (analyze(state, scope, as_expr(node)))
return -1;
if (analyze_type(state, scope, as_type(node)))
return -1;
/* for now, any 'register' size type can be cast to any other. Might
* restrict this, like signed to unsigned might be weird if its negative
* etc and should be done via a helper function */
struct type *expr_type = (as_expr(node))->t;
struct type *type = as_type(node);
if (!castable_type(expr_type)) {
char *s = type_str(expr_type);
semantic_error(scope, as_expr(node), "can't cast from %s\n", s);
free(s);
return -1;
}
if (!castable_type(type)) {
char *s = type_str(type);
type_error(scope, type, "can't cast to %s\n", s);
free(s);
return -1;
}
node->t = type;
return 0;
}
static int analyze_assign(struct state *state, struct scope *scope, struct ast *node)
{
struct ast *expr = assign_expr(node);
if (analyze(state, scope, expr))
return -1;
struct ast *target = assign_target(node);
if (analyze(state, scope, target))
return -1;
if (!types_match(expr->t, target->t)) {
type_mismatch(scope, node, target->t, expr->t);
return -1;
}
if (!is_lvalue(target)) {
semantic_error(scope, target, "not an lvalue");
return -1;
}
node->t = target->t;
return 0;
}
static int analyze_dot(struct state *state, struct scope *scope, struct ast *node)
{
struct ast *expr = dot_expr(node);
if (analyze(state, scope, expr))
return -1;
struct type *type = expr->t;
if (type->k != TYPE_ID) {
char *s = type_str(type);
semantic_error(scope, node, "type %s does not have any members", s);
free(s);
return -1;
}
struct ast *def = file_scope_find_type(scope, tid_str(type));
if (!def) {
semantic_error(scope, node, "no such type: %s", tid_str(type));
return -1;
}
if (def->k != AST_STRUCT_DEF) {
semantic_error(scope, node, "not a struct type: %s", tid_str(type));
return -1;
}
struct ast *member = scope_find_symbol(def->scope, dot_id(node));
if (!member) {
semantic_error(scope, node, "no such member: %s", dot_id(node));
return -1;
}
node->t = member->t;
return 0;
}
static int analyze_sizeof(struct state *state, struct scope *scope, struct ast *node)
{
if (analyze_type(state, scope, sizeof_type(node)))
return -1;
/* hmm, no built-in size_t */
node->t = tgen_type(TYPE_U64, NULL, NULL, node->loc);
return 0;
}
static int analyze_arr(struct state *state, struct scope *scope, struct ast *node)
{
struct ast *base = arr_base(node);
if (analyze(state, scope, base))
return -1;
if (base->t->k != TYPE_ARR) {
char *s = type_str(base->t);
semantic_error(scope, base, "expected array type, got %s", s);
free(s);
return -1;
}
struct ast *idx = arr_idx(node);
if (analyze(state, scope, idx))
return -1;
if (!is_int_type(idx->t->k)) {
char *s = type_str(idx->t);
semantic_error(scope, idx, "expected int type, got %s", s);
free(s);
return -1;
}
node->t = tarr_base(base->t);
return 0;
}
static int analyze_nil_check(struct state *state, struct scope *scope, struct ast *node)
{
struct ast *expr = nil_check_expr(node);
if (analyze(state, scope, expr))
return -1;
if (!is_ptr_type(expr->t->k)) {
char *s = type_str(expr->t);
semantic_error(scope, expr, "expected ptr type, got %s", s);
free(s);
return -1;
}
if (analyze(state, scope, nil_check_body(node)))
return -1;
struct ast *var = nil_check_ref(node);
struct type *base = tptr_base(expr->t);
struct type *ref = tgen_ref(clone_type(base), node->loc);
if (!var_type(var))
var_type(var) = ref;
if (analyze(state, scope, var))
return -1;
if (!types_match(ref, var->t)) {
type_mismatch(scope, var, ref, var->t);
return -1;
}
node->t = tgen_void(node->loc);
return 0;
}
static int analyze_forget(struct state *state, struct scope *scope, struct ast *node)
{
struct ast *def = file_scope_find_symbol(scope, forget_id(node));
if (!def) {
semantic_error(scope, node, "no such symbol");
return -1;
}
node->t = tgen_void(node->loc);
return 0;
}
static int analyze(struct state *state, struct scope *scope, struct ast *node)
{
if (!node)
return 0;
if (ast_flags(node, AST_FLAG_ANALYZED)) {
assert(node->t);
return 0;
}
if (ast_flags(node, AST_FLAG_PREANALYZIS)) {
semantic_error(scope, node, "semantic loop detected");
return -1;
}
ast_set_flags(node, AST_FLAG_PREANALYZIS);
if (!node->scope)
node->scope = scope;
int ret = 0;
if (is_binop(node)) {
ret = analyze_binop(state, scope, node);
goto out;
}
if (is_comparison(node)) {
ret = analyze_comparison(state, scope, node);
goto out;
}
if (is_unop(node)) {
ret = analyze_unop(state, scope, node);
goto out;
}
switch (node->k) {
case AST_CLOSURE: ret = analyze_closure(state, scope, node); break;
case AST_PROC_DEF: ret = analyze_proc(state, scope, node); break;
case AST_VAR_DEF: ret = analyze_var(state, scope, node); break;
case AST_BLOCK: ret = analyze_block(state, scope, node); break;
case AST_DEREF: ret = analyze_deref(state, scope, node); break;
case AST_INIT: ret = analyze_init(state, scope, node); break;
case AST_CALL: ret = analyze_call(state, scope, node); break;
case AST_REF: ret = analyze_ref(state, scope, node); break;
case AST_LET: ret = analyze_let(state, scope, node); break;
case AST_ID: ret = analyze_id(state, scope, node); break;
case AST_IF: ret = analyze_if(state, scope, node); break;
case AST_NIL_CHECK: ret = analyze_nil_check(state, scope, node); break;
case AST_CONST_INT: ret = analyze_int(state, scope, node); break;
case AST_CONST_STR: ret = analyze_str(state, scope, node); break;
case AST_INSTANCE: ret = analyze_instance(state, scope, node); break;
case AST_TEMPLATE: ret = analyze_template(state, scope, node); break;
case AST_STRUCT_DEF: ret = analyze_struct(state, scope, node); break;
case AST_CONSTRUCT: ret = analyze_construct(state, scope, node); break;
case AST_SIZEOF: ret = analyze_sizeof(state, scope, node); break;
case AST_NIL: ret = analyze_nil(state, scope, node); break;
case AST_AS: ret = analyze_as(state, scope, node); break;
case AST_ASSIGN: ret = analyze_assign(state, scope, node); break;
case AST_ARR: ret = analyze_arr(state, scope, node); break;
case AST_DOT: ret = analyze_dot(state, scope, node); break;
case AST_FORGET: ret = analyze_forget(state, scope, node); break;
case AST_TRAIT_DEF:
node->t = tgen_void(node->loc);
ret = 0;
break;
case AST_EMPTY:
node->t = tgen_void(node->loc);
ret = 0;
break;
case AST_IMPORT:
node->t = tgen_void(node->loc);
ret = 0;
break;
default:
internal_error("missing ast analysis for %s", ast_str(node->k));
return -1;
}
out:
if (ret)
return ret;
assert(node->t);
assert(node->scope);
ast_set_flags(node, AST_FLAG_ANALYZED);
return 0;
}
static int analyze_list(struct state *state, struct scope *scope,
struct ast *nodes)
{
foreach_node(node, nodes) {
if (analyze(state, scope, node))
return -1;
}
return 0;
}
static int analyze_type(struct state *state, struct scope *scope,
struct type *type)
{
if (type->t0 && analyze_type(state, scope, type->t0))
return -1;
/* temporary */
if (!type->id)
return 0;
char *id = type->id;
/* handle primitives */
if (strcmp(id, "i8") == 0) {
type->k = TYPE_I8;
return 0;
}
if (strcmp(id, "u8") == 0) {
type->k = TYPE_U8;
return 0;
}
if (strcmp(id, "i16") == 0) {
type->k = TYPE_I16;
return 0;
}
if (strcmp(id, "u16") == 0) {
type->k = TYPE_U16;
return 0;
}
if (strcmp(id, "i32") == 0) {
type->k = TYPE_I32;
return 0;
}
if (strcmp(id, "u32") == 0) {
type->k = TYPE_U32;
return 0;
}
if (strcmp(id, "i64") == 0) {
type->k = TYPE_I64;
return 0;
}
if (strcmp(id, "u64") == 0) {
type->k = TYPE_U64;
return 0;
}
/* check for user-defined types */
struct ast *def = file_scope_find_type(scope, id);
if (!def) {
type_error(scope, type, "no such type: %s", id);
return -1;
}
/* don't change type->k since we might want to check up on this again
* later */
return 0;
}
static int analyze_type_list(struct state *state, struct scope *scope,
struct type *types)
{
foreach_type(type, types) {
if (analyze_type(state, scope, type))
return -1;
}
return 0;
}
static int copy_scope(bool reexport, struct scope *to, struct scope *from)
{
foreach(visible, symbol, &from->symbols) {
struct ast *def = symbol->data;
if (!reexport && def->scope != from)
continue;
if (!ast_flags(def, AST_FLAG_PUBLIC))
continue;
if (scope_add_symbol(to, def))
return -1;
}
foreach(visible, type, &from->types) {
struct ast *def = type->data;
if (!reexport && def->scope != from)
continue;
if (!ast_flags(def, AST_FLAG_PUBLIC))
continue;
if (scope_add_type(to, def))
return -1;
}
foreach(visible, trait, &from->traits) {
struct ast *def = trait->data;
if (!reexport && def->scope != from)
continue;
if (!ast_flags(def, AST_FLAG_PUBLIC))
continue;
if (scope_add_trait(to, def))
return -1;
}
foreach(visible, template, &from->templates) {
struct ast *def = template->data;
if (!reexport && def->scope != from)
continue;
if (!ast_flags(def, AST_FLAG_PUBLIC))
continue;
if (scope_add_template(to, def))
return -1;
}
return 0;
}
/* allowed to be noisy */
static int try_import_file(struct scope *scope, struct ast *node)
{
const char *file = import_file(node);
struct scope *child = compile_file(file);
if (!child) {
semantic_info(scope, node, "imported here");
return -1;
}
if (copy_scope(ast_flags(node, AST_FLAG_PUBLIC), scope, child)) {
semantic_info(scope, node, "imported here");
return -1;
}
return 0;
}
/* should be quiet upon failure */
static int try_import_mod(struct scope *scope, struct ast *node)
{
/** @todo cleanup */
void *mod = dlopen(import_file(node), RTLD_LAZY);
if (!mod)
return -1;
fwd_open_t fwdopen = dlsym(mod, "fwdopen");
if (!fwdopen) {
dlclose(mod);
return -1;
}
mod_vec_append(&scope->mods, mod);
return fwdopen((void *)scope);
}
static struct type *fwd_type_kind(fwd_type_t type)
{
switch (type) {
case FWD_VOID: return tgen_void(NULL_LOC());
case FWD_I64: return tgen_type(TYPE_I64, NULL, NULL, NULL_LOC());
case FWD_U64: return tgen_type(TYPE_U64, NULL, NULL, NULL_LOC());
case FWD_PTR: {
/* hack? */
return tgen_nil(NULL_LOC());
}
default:
break;
}
abort();
return NULL;
}
int fwd_register(struct fwd_state *state, const char *name, fwd_extern_t func, fwd_type_t rtype, ...)
{
struct scope *scope = (void *)state;
struct ast *vars = NULL;
va_list args;
va_start(args, rtype);
size_t idx = 0;
while (1) {
fwd_type_t type = va_arg(args, enum fwd_type);
if (type == FWD_END)
break;
/* eight bytes is likely more than enough, since first three are
* var, then we have four bytes for indexes and one trailing
* NULL */
char *name = calloc(8, sizeof(char));
assert(name);
sprintf(name, "%s%zu", "var", idx);
struct ast *new = gen_var(name, fwd_type_kind(type), NULL_LOC());
if (vars)
new->n = vars;
vars = new;
idx++;
}
va_end(args);
/* append 'return' as a continuation */
if (rtype != FWD_VOID) {
char *name = strdup("ret");
struct ast *new = gen_var(name,
tgen_closure(fwd_type_kind(rtype), NULL_LOC()),
NULL_LOC());
if (vars)
new->n = vars;
vars = new;
}
vars = reverse_ast_list(vars);
struct ast *def = gen_proc(strdup(name), vars,
fwd_type_kind(rtype), NULL, NULL_LOC());
if (scope_add_symbol(scope, def))
return -1;
return 0;
}
int analyze_root(struct scope *scope, struct ast *root)
{
foreach_node(node, root) {
switch (node->k) {
case AST_PROC_DEF:
if (scope_add_symbol(scope, node))
return -1;
break;
case AST_STRUCT_DEF:
if (scope_add_type(scope, node))
return -1;
break;
case AST_STRUCT_CONT:
abort();
break;
case AST_TRAIT_DEF:
if (scope_add_trait(scope, node))
return -1;
break;
case AST_IMPORT: {
if (!try_import_mod(scope, node))
break;
if (!try_import_file(scope, node))
break;
return -1;
}
case AST_TEMPLATE:
if (scope_add_template(scope, node))
return -1;
break;
case AST_SUPERTEMPLATE:
if (scope_add_template(scope, node))
return -1;
break;
case AST_INSTANCE:
break;
default:
abort();
}
}
foreach_node(node, root) {
struct state state = {};
if (analyze(&state, scope, node))
return -1;
}
return 0;
}
|