/
checker.v
2327 lines (2245 loc) · 59.9 KB
/
checker.v
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
// Copyright (c) 2020-2024 Joe Conigliaro. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module types
import time
import v2.ast
import v2.errors
import v2.pref
import v2.token
struct Environment {
mut:
// errors with no default value
scopes shared map[string]&Scope = map[string]&Scope{}
// types map[int]Type
// TODO:
// methods map...
// methods map[int][]&Fn
methods map[string][]&Fn
generic_types map[string][]map[string]Type
cur_generic_types []map[string]Type
}
pub fn Environment.new() &Environment {
return &Environment{}
}
enum DeferredKind {
fn_decl
fn_decl_generic
struct_decl
const_decl
}
struct Deferred {
kind DeferredKind
func fn () = unsafe { nil }
scope &Scope
}
struct Checker {
pref &pref.Preferences
// info Info
// TODO: mod
mod &Module = new_module('main', '')
mut:
env &Environment = &Environment{}
file_set &token.FileSet
scope &Scope = new_scope(unsafe { nil })
c_scope &Scope = new_scope(unsafe { nil })
deferred []Deferred
expected_type ?Type
generic_params []string
// TODO: remove once fields/methods with same name
// are no longer allowed & removed.
expecting_method bool
}
pub fn Checker.new(prefs &pref.Preferences, file_set &token.FileSet, env &Environment) &Checker {
return &Checker{
pref: unsafe { prefs }
file_set: unsafe { file_set }
env: unsafe { env }
}
}
pub fn (mut c Checker) get_module_scope(module_name string, parent &Scope) &Scope {
// return c.env.scopes[module_name] or {
// s := new_scope(parent)
// c.env.scopes[module_name] = s
// s
// }
return lock c.env.scopes {
c.env.scopes[module_name] or {
s := new_scope(parent)
c.env.scopes[module_name] = s
s
}
}
}
pub fn (mut c Checker) check_files(files []ast.File) {
// c.file_set = unsafe { file_set }
c.preregister_all_scopes(files)
c.preregister_all_types(files)
for file in files {
c.check_file(file)
}
// c.log('DEFERRED - START')
// TODO: a better way to do this, please.
// ideally just resolve what needs to be
// const decl
for d in c.deferred {
if d.kind != .const_decl {
continue
}
c.scope = d.scope
d.func()
}
// fn decls
for d in c.deferred {
if d.kind != .fn_decl {
continue
}
c.scope = d.scope
d.func()
}
// fn decls - generic
for d in c.deferred {
if d.kind != .fn_decl_generic {
continue
}
c.scope = d.scope
d.func()
}
// c.log('DEFERRED - END')
}
pub fn (mut c Checker) check_file(file ast.File) {
if !c.pref.verbose {
unsafe {
goto start_no_time
}
}
mut sw := time.new_stopwatch()
start_no_time:
// file_scope := new_scope(c.mod.scope)
// mut mod_scope := new_scope(c.mod.scope)
// c.env.scopes[file.mod] = mod_scope
mut mod_scope := lock c.env.scopes {
c.env.scopes[file.mod] or {
panic('not found for mod: ${file.mod}')
// c.env.scopes[file.mod] = c.mod.scope
// c.mod.scope
}
}
c.scope = mod_scope
// mut mod_scope := c.env.scopes[file.mod] or {
// panic('scope should exist')
// }
// c.scope = mod_scope
for stmt in file.stmts {
// if stmt is ast.Decl {
match stmt {
ast.EnumDecl, ast.InterfaceDecl, ast.StructDecl, ast.TypeDecl {
continue
}
else {
c.decl(stmt)
}
}
// }
}
for stmt in file.stmts {
c.stmt(stmt)
}
if c.pref.verbose {
check_time := sw.elapsed()
println('type check ${file.name}: ${check_time.milliseconds()}ms (${check_time.microseconds()}µs)')
}
}
fn (mut c Checker) preregister_scopes(file ast.File) {
builtin_scope := c.get_module_scope('builtin', universe)
mod_scope := c.get_module_scope(file.mod, builtin_scope)
c.scope = mod_scope
// add self (own module) for constants. can use own module prefix inside module
c.scope.insert(file.mod, Module{ scope: c.get_module_scope(file.mod, builtin_scope) })
// add imports
for imp in file.imports {
mod := if imp.is_aliased { imp.name.all_after_last('.') } else { imp.alias }
c.scope.insert(imp.alias, Module{ scope: c.get_module_scope(mod, builtin_scope) })
}
// add C
c.scope.insert('C', Module{ scope: c.c_scope })
}
fn (mut c Checker) preregister_all_scopes(files []ast.File) {
// builtin_scope := c.get_module_scope('builtin', universe)
// preregister scopes & imports
for file in files {
c.preregister_scopes(file)
// mod_scope := c.get_module_scope(file.mod, builtin_scope)
// c.scope = mod_scope
// // add self (own module) for constants
// c.scope.insert(file.mod, Module{scope: c.get_module_scope(file.mod, builtin_scope)})
// // add imports
// for imp in file.imports {
// mod := if imp.is_aliased { imp.name.all_after_last('.') } else { imp.alias }
// c.scope.insert(imp.alias, Module{scope: c.get_module_scope(mod, builtin_scope)})
// }
// // add C
// c.scope.insert('C', Module{scope: c.c_scope})
}
}
fn (mut c Checker) preregister_types(file ast.File) {
mut mod_scope := c.env.scopes[file.mod] or { panic('scope should exist') }
c.scope = mod_scope
for stmt in file.stmts {
// if stmt is ast.Decl {
match stmt {
ast.EnumDecl, ast.InterfaceDecl, ast.StructDecl, ast.TypeDecl {}
else {
continue
}
}
c.decl(stmt)
// }
}
}
fn (mut c Checker) preregister_all_types(files []ast.File) {
for file in files {
c.preregister_types(file)
}
// c.log('DEFERRED - START')
for d in c.deferred {
if d.kind != .struct_decl {
continue
}
c.scope = d.scope
d.func()
}
// c.log('DEFERRED - END')
}
fn (mut c Checker) decl(decl ast.Stmt) {
match decl {
ast.ConstDecl {
for field in decl.fields {
// c.log('const decl: $field.name')
obj := Const{
mod: c.mod
name: field.name
// typ: c.expr(field.value)
}
c.scope.insert(obj.name, obj)
// TODO: check if constains references to other consts and only
// delay those. or keep dererring until type is known othewise error
// work out best way to do this, and use same approach for everything
c.later(fn [mut c, field] () {
// c.log('updating const $field.name type')
const_type := c.expr(field.value)
if mut cd := c.scope.lookup(field.name) {
if mut cd is Const {
cd.typ = const_type.typed_default()
}
}
}, .const_decl)
}
}
ast.EnumDecl {
// TODO: if non builtin types can be used as part of
// the expr then these will need to get delayed also.
mut fields := []Field{}
for field in decl.fields {
fields << Field{
name: field.name
}
}
// as_type := decl.as_type !is ast.EmptyExpr { c.expr(decl.as_type) } else { Type(int_) }
obj := Enum{
is_flag: decl.attributes.has('flag')
name: decl.name
fields: fields
}
c.scope.insert(obj.name, Type(obj))
}
ast.FnDecl {
// if decl.typ.generic_params.len > 0 {
// fn_decl := decl
// c.later(fn[mut c, fn_decl]() {
// c.fn_decl(fn_decl)
// }, .fn_decl_generic)
// } else {
// c.fn_decl(decl)
// }
c.fn_decl(decl)
}
ast.GlobalDecl {
// for field in decl.fields {
// // field_type := c.expr(field.typ)
// // if field.name == 'g_timers' {
// // // dump(field.typ)
// // panic('# g_timers: $field_type.name()')
// // }
// obj := Global{
// name: field.name
// // typ: c.expr(field.typ)
// typ: c.expr(field.value)
// }
// // c.log('GlobalDecl: $field.name - $obj.typ.type_name()')
// c.scope.insert(field.name, obj)
// }
}
ast.InterfaceDecl {
// TODO:
obj := Interface{
name: decl.name
}
c.scope.insert(decl.name, Type(obj))
interface_decl := decl
mut scope := c.scope
c.later(fn [mut c, mut scope, interface_decl] () {
mut fields := []Field{}
for field in interface_decl.fields {
fields << Field{
name: field.name
typ: c.expr(field.typ)
}
}
if mut id := scope.lookup(interface_decl.name) {
if mut id is Type {
if mut id is Interface {
id.fields = fields
}
}
}
}, .struct_decl)
}
ast.StructDecl {
// c.log(' # StructDecl: $decl.name')
// TODO: clean this up
struct_decl := decl
c.later(fn [mut c, struct_decl] () {
// c.log('add fields: $struct_decl.name')
mut fields := []Field{}
for field in struct_decl.fields {
fields << Field{
name: field.name
typ: c.expr(field.typ)
}
}
mut embedded := []Struct{}
for embedded_expr in struct_decl.embedded {
embedded_type := c.expr(embedded_expr)
if embedded_type is Struct {
embedded << embedded_type
} else {
c.error_with_pos('can only structs, `${embedded_type.name()}` is not a struct.',
struct_decl.pos)
}
}
mut update_scope := if struct_decl.language == .c { c.c_scope } else { c.scope }
// TODO: work best way to do this?
// modify the original type since, that is
// the one every Type will be pointing to
if mut sd := update_scope.lookup(struct_decl.name) {
if mut sd is Type {
if mut sd is Struct {
sd.fields = fields
sd.embedded = embedded
}
}
}
// obj := types.Struct{
// name: struct_decl.name
// fields: fields
// }
// typ := Type(obj)
// c.scope.insert(struct_decl.name, typ)
}, .struct_decl)
// c.log('struct decl: $decl.name')
obj := Struct{
name: decl.name
// fields: [Field{name: 'len', typ: ast.Ident{name: 'int'}}]
// fields: [Field{name: 'len'}]
}
mut typ := Type(obj)
// TODO: proper
if decl.language == .c {
c.c_scope.insert(decl.name, typ)
} else {
c.scope.insert(decl.name, typ)
}
}
ast.TypeDecl {
type_decl := decl
// alias
if decl.variants.len == 0 {
alias_type := Alias{
name: decl.name
// TODO: defer
// parent: c.expr(decl.base_type)
}
mut typ := Type(alias_type)
c.scope.insert(decl.name, typ)
c.later(fn [mut c, type_decl] () {
// mut obj := c.scope.lookup(type_decl.name) or { panic(err.msg()) }
// mut typ := obj.typ()
// if mut typ is Alias {
// typ.base_type = c.expr(type_decl.base_type)
// }
if mut obj := c.scope.lookup(type_decl.name) {
if mut obj is Type {
if mut obj is Alias {
obj.base_type = c.expr(type_decl.base_type)
}
}
}
}, .struct_decl)
}
// sum type
else {
sum_type := SumType{
name: decl.name
// variants: decl.variants
}
mut typ := Type(sum_type)
c.scope.insert(decl.name, typ)
c.later(fn [mut c, type_decl] () {
mut obj := c.scope.lookup(type_decl.name) or { panic(err.msg()) }
mut typ := obj.typ()
// mut typ := c.expr(ast.Ident{name: type_decl.name})
if mut typ is SumType {
for variant in type_decl.variants {
typ.variants << c.expr(variant)
}
}
}, .struct_decl)
}
}
else {}
}
}
fn (mut c Checker) check_types(exp_type Type, got_type Type) bool {
// TODO: will this work with Primitive? might need to add comparison methods
if got_type == exp_type {
return true
}
// number literals
if exp_type.is_number() && got_type.is_number_literal() {
return true
}
// primitives
if exp_type is Primitive && got_type is Primitive {
// mut got_props := got_type.props
// got_props.clear(.untyped)
// // checks if both match flot or int
// if got_props == exp_type.props {
// return true
// }
}
return false
}
fn (mut c Checker) expr(expr ast.Expr) Type {
c.log('expr: ${expr.type_name()}')
match expr {
ast.ArrayInitExpr {
// c.log('ArrayInit:')
// `[1,2,3,4]`
if expr.exprs.len > 0 {
is_fixed := expr.len !is ast.EmptyExpr
// TODO: check all exprs
first_elem_type := c.expr(expr.exprs.first())
// NOTE: why did I have this shortcut here?
// if expr.exprs.len == 1 {
// if first_elem_type.is_number_literal() {
// return Array{
// elem_type: int_
// }
// }
// }
// TODO: promote [0] - proper
expected_type_prev := c.expected_type
expected_type := c.expected_type or {
// `[Type.value_a, .value_b]`
// set expected type for checking `.value_b`
if first_elem_type is Enum {
c.expected_type = first_elem_type
}
first_elem_type
}
for i, elem_expr in expr.exprs {
if i == 0 {
continue
}
mut elem_type := c.expr(elem_expr)
// TODO: best way to handle this?
if elem_type.is_number_literal() && first_elem_type.is_number() {
elem_type = first_elem_type
}
// sum type, check variants
if (expected_type is SumType && elem_type !in expected_type.variants)
&& elem_type != first_elem_type // everyting else
{
// TOOD: add generl method for promotion/coersion
c.error_with_pos('expecting element of type: ${first_elem_type.name()}, got ${elem_type.name()}',
expr.pos)
}
}
c.expected_type = expected_type_prev
return if is_fixed {
ArrayFixed{
len: expr.exprs.len
elem_type: first_elem_type
}
} else {
Array{
elem_type: first_elem_type
}
}
}
// `[]int{}`
return c.expr(expr.typ)
}
ast.AsCastExpr {
// TODO:
c.expr(expr.expr)
return c.expr(expr.typ)
}
ast.BasicLiteral {
// c.log('ast.BasicLiteral: $expr.kind.str(): $expr.value')
match expr.kind {
.char {
return char_
}
.key_false, .key_true {
return bool_
}
// TODO:
.number {
// TODO: had to be a better way to do this
// should this be handled earlier? scanner?
if expr.value.contains('.') {
return float_literal_
}
return int_literal_
}
else {
panic('invalid ast.BasicLiteral kind: ${expr.kind}')
}
}
}
ast.CallOrCastExpr {
// c.log('CallOrCastExpr: $lhs_type.name()')
// lhs_type := c.expr(expr.lhs)
// // call
// if lhs_type is FnType {
// return c.call_expr(ast.CallExpr{lhs: expr.lhs, args: [expr.expr]})
// }
// // cast
// // expr_type := c.expr(expr.expr)
// // TODO: check if expr_type can be cast to lhs_type
// return lhs_type
return c.expr(c.resolve_call_or_cast_expr(expr))
}
ast.CallExpr {
// TOOD/FIXME: proper
// we need a way to handle C.stat|sigaction() / C.stat|sigaction{}
// multiple items with same name inside scope lookup.
if expr.lhs is ast.SelectorExpr {
if expr.lhs.rhs.name == 'stat' {
return int_
}
}
return c.call_expr(expr)
}
ast.CastExpr {
typ := c.expr(expr.typ)
c.log('CastExpr: ${typ.name()}')
return typ
}
ast.ComptimeExpr {
cexpr := c.resolve_expr(expr.expr)
// TODO: move to checker, where `ast.*Or*` nodes will be resolved.
if cexpr !in [ast.CallExpr, ast.IfExpr] {
c.error_with_pos('unsupported comptime: ${cexpr.type_name()}', expr.pos)
}
// TODO: $if dynamic_bohem ...
if cexpr is ast.IfExpr {
c.log('TODO: comptime IfExpr')
return void_
}
c.log('ComptimeExpr: ' + cexpr.type_name())
// return c.expr(cexpr)
}
ast.EmptyExpr {
// TODO:
return void_
}
ast.FnLiteral {
return c.fn_type(expr.typ, FnTypeAttribute.empty)
}
ast.GenericArgs {
// NOTE: first generic args handled in CallExpr
// this is generic struct nested in generic args
mut name := ''
match expr.lhs {
ast.Ident {
name = expr.lhs.name
}
else {}
}
mut args := []string{}
for arg in expr.args {
if arg is ast.Ident {
args << arg.name
}
}
lhs_type := c.expr(expr.lhs)
if lhs_type is FnType {
return FnType{
...lhs_type
generic_params: args
}
}
return Struct{
name: name
generic_params: args
}
}
ast.GenericArgOrIndexExpr {
return c.expr(c.resolve_generic_arg_or_index_expr(expr))
}
ast.Ident {
// c.log('ident: $expr.name')
obj := c.ident(expr)
typ := obj.typ()
// TODO:
if expr.name == 'string' {
if typ is Struct {
return string_
}
}
return typ
}
ast.IfExpr {
// if guard
if expr.cond is ast.IfGuardExpr {
c.open_scope()
c.assign_stmt(expr.cond.stmt, true)
c.stmt_list(expr.stmts)
c.close_scope()
if expr.else_expr is ast.IfExpr {
c.open_scope()
c.scope.insert('err', Type(Struct{ name: 'Error' }))
c.stmt_list(expr.else_expr.stmts)
c.close_scope()
}
// TODO: never used, add a special type?
return void_
}
// normal if
return c.if_expr(expr)
}
ast.IfGuardExpr {
c.assign_stmt(expr.stmt, true)
// TODO:
return bool_
}
ast.IndexExpr {
lhs_type := c.expr(expr.lhs)
// TODO: make sure lhs_type is indexable
// if !lhs_type.is_indexable() { c.error('cannot index ${lhs_type.name()}') }
value_type := if expr.expr is ast.RangeExpr {
lhs_type
} else {
lhs_type.value_type()
}
// c.log('IndexExpr: ${value_type.name()} / ${lhs_type.name()}')
return value_type
}
ast.InfixExpr {
lhs_type := c.expr(expr.lhs)
// TODO: why was I setting expected type for enum here?
// expected_type := c.expected_type
// if lhs_type is Enum {
// c.expected_type = lhs_type
// }
c.expr(expr.rhs)
// c.expected_type = expected_type
if expr.op.is_comparison() {
return bool_
}
return lhs_type
}
ast.InitExpr {
// TODO: try handle this from expr
// mut typ_expr := expr.typ
// if expr.typ is ast.GenericArgs {
// typ_expr = expr.typ.lhs
// }
typ := c.expr(expr.typ)
// TODO:
// if typ is ast.ChannelType {
// for field in expr.fields {
// match field.name {
// 'cap' {}
// else { c.error_with_pos('unknown channel attribute `${key}`') }
// }
// }
// }
// TODO:
// for field in expr.fields {
// if field.value !is ast.EmptyExpr {
// field_expr_type := c.expr(field.value)
// }
// }
return typ
}
ast.KeywordOperator {
// TODO:
typ := c.expr(expr.exprs[0])
match expr.op {
.key_go, .key_spawn {
return Thread{}
}
else {
return typ
}
}
}
ast.MapInitExpr {
// TOOD: type check keys/vals
// `map[type]type{}`
if expr.typ !is ast.EmptyExpr {
typ := c.expr(expr.typ)
return typ
}
// `{}`
if expr.keys.len == 0 {
return c.expected_type or {
c.error_with_pos('empty map {} used in unsupported context', expr.pos)
}
}
// `{key: value}`
key0_type := c.expr(expr.keys[0])
value0_type := c.expr(expr.vals[0])
return Map{
key_type: key0_type
value_type: value0_type
}
}
ast.MatchExpr {
return c.match_expr(expr, true)
}
ast.ModifierExpr {
// if expr.expr !is ast.Ident && expr.expr !is ast.Type {
// panic('not ident: $expr.expr.type_name()')
// }
return c.expr(expr.expr)
}
ast.OrExpr {
cond := c.resolve_expr(expr.expr)
cond_type := c.expr(cond).unwrap()
// c.log('OrExpr: ${cond_type.name()}')
if expr.stmts.len > 0 {
last_stmt := expr.stmts.last()
if last_stmt is ast.ExprStmt {
expr_stmt_type := c.expr(last_stmt.expr).unwrap()
// c.log('OrExpr: last_stmt_type: ${cond_type.name()}')
// TODO: non returning call (currently just checking void)
// should probably lookup function/method and check for noreturn attribute?
// if cond is ast.CallExpr {}
// do we need to do promotion here
// last stmt expr does does not return a type
if expr_stmt_type !is Void && !c.check_types(cond_type, expr_stmt_type) {
c.error_with_pos('or expr expecting ${cond_type.name()}, got ${expr_stmt_type.name()}',
expr.pos)
}
return cond_type
}
}
return cond_type
}
ast.ParenExpr {
return c.expr(expr.expr)
}
ast.PostfixExpr {
// TODO:
// typ := c.expr(expr.expr)
// if typ is FnType {
// if rt := typ.return_type {
// if rt in [OptionType, ResultType] { return typ }
// }
// if expr.op == .not {
// return_type := OptionType{base_type: typ.return_type or { void_ }}
// return FnType{...typ, return_type: return_type}
// }
// else if expr.op == .question {
// return_type := ResultType{base_type: typ.return_type or { void_ }}
// return FnType{...typ, return_type: return_type}
// }
// }
// return typ
return c.expr(expr.expr)
}
ast.PrefixExpr {
expr_type := c.expr(expr.expr)
if expr.op == .amp {
return Pointer{
base_type: expr_type
}
} else if expr.op == .mul {
if expr_type is Pointer {
// c.log('DEREF')
return expr_type.base_type
} else {
c.error_with_pos('deref on non pointer type `${expr_type.name()}`',
expr.pos)
}
}
return c.expr(expr.expr)
}
ast.RangeExpr {
c.expr(expr.start)
c.expr(expr.end)
return Type(Array{
elem_type: int_
})
}
ast.SelectExpr {
c.stmt_list(expr.stmts)
}
ast.SelectorExpr {
// enum value: `.green`
if expr.lhs is ast.EmptyExpr {
// c.log('got enum value')
// // dump(expr)
// return c.expected_type
return c.expected_type or {
c.error_with_pos('c.expected_type is not set', expr.pos)
}
// return int_
}
// normal selector
return c.selector_expr(expr)
}
ast.StringInterLiteral {
// TODO:
return string_
}
ast.StringLiteral {
return string_
}
ast.Tuple {
mut types := []Type{}
for x in expr.exprs {
types << c.expr(x)
}
return Tuple{
types: types
}
}
ast.Type {
match expr {
ast.AnonStructType {}
ast.ArrayType {
return Array{
elem_type: c.expr(expr.elem_type)
}
}
ast.ArrayFixedType {
// TODO:
return Array{
elem_type: c.expr(expr.elem_type)
}
}
ast.ChannelType {
return Channel{
elem_type: if expr.elem_type !is ast.EmptyExpr {
c.expr(expr.elem_type)
} else {
none
}
}
}
ast.FnType {
return c.fn_type(expr, FnTypeAttribute.empty)
}
ast.GenericType {
// TODO:
return c.expr(expr.name)
// return c
}
ast.MapType {
return Map{
key_type: c.expr(expr.key_type)
value_type: c.expr(expr.value_type)
}
}
ast.NilType {
return nil_
}
ast.NoneType {
return none_
}
ast.OptionType {
return OptionType{
base_type: c.expr(expr.base_type)
}
}
ast.ResultType {
return ResultType{
base_type: c.expr(expr.base_type)
}
}
ast.ThreadType {
return Thread{
elem_type: if expr.elem_type !is ast.EmptyExpr {
c.expr(expr.elem_type)
} else {
none
}
}
}
ast.TupleType {
mut types := []Type{}
for tx in expr.types {
types << c.expr(tx)
}
return Tuple{
types: types
}
}
// else{
// c.log('expr.Type: not implemented ${expr.type_name()} - ${typeof(expr).name}')
// }
}
}
ast.UnsafeExpr {
// TODO: proper
c.stmt_list(expr.stmts)
last_stmt := expr.stmts.last()
if last_stmt is ast.ExprStmt {
return c.expr(last_stmt.expr)
}
// TODO: impl: avoid returning types everywhere / using void
// perhaps use a struct and set the type and other info in it when needed
return void_
}
else {}
}
// TOODO: remove (add all variants)
c.log('expr: unhandled ${expr.type_name()}')
return int_
}
fn (mut c Checker) stmt(stmt ast.Stmt) {
match stmt {
ast.AssertStmt {
c.expr(stmt.expr)
}
ast.AssignStmt {
c.assign_stmt(stmt, false)
}
ast.BlockStmt {
c.stmt_list(stmt.stmts)
}
// ast.Decl {
// // Handled earlier
// // match stmt {
// // ast.FnDecl{}
// // ast.TypeDecl {}
// // else {}
// // }
// }
ast.GlobalDecl {
for field in stmt.fields {
// c.log('GlobalDecl: $field.name - $obj.typ.type_name()')
field_type := if field.typ !is ast.EmptyExpr {
c.expr(field.typ)
} else {
c.expr(field.value)
}
obj := Global{
name: field.name
typ: field_type
}
c.scope.insert(field.name, obj)
}
}
ast.DeferStmt {
c.stmt_list(stmt.stmts)
}
ast.ExprStmt {
if stmt.expr is ast.MatchExpr {
c.match_expr(stmt.expr, false)
} else {
c.expr(stmt.expr)
}
}
ast.ForStmt {
c.open_scope()
// TODO: vars for other for loops