forked from nsf/gocode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decl.go
1373 lines (1244 loc) · 29.8 KB
/
decl.go
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
package main
import (
"bytes"
"fmt"
"go/ast"
"go/token"
"io"
"reflect"
"strings"
"sync"
)
// decl.class
type decl_class int16
const (
decl_invalid = decl_class(-1 + iota)
// these are in a sorted order
decl_const
decl_func
decl_package
decl_type
decl_var
// this one serves as a temporary type for those methods that were
// declared before their actual owner
decl_methods_stub
)
func (this decl_class) String() string {
switch this {
case decl_invalid:
return "PANIC"
case decl_const:
return "const"
case decl_func:
return "func"
case decl_package:
return "package"
case decl_type:
return "type"
case decl_var:
return "var"
case decl_methods_stub:
return "IF YOU SEE THIS, REPORT A BUG" // :D
}
panic("unreachable")
}
// decl.flags
type decl_flags int16
const (
decl_foreign = decl_flags(1 << iota) // imported from another package
// means that the decl is a part of the range statement
// its type is inferred in a special way
decl_rangevar
// for preventing infinite recursions and loops in type inference code
decl_visited
)
//-------------------------------------------------------------------------
// decl
//
// The most important data structure of the whole gocode project. It
// describes a single declaration and its children.
//-------------------------------------------------------------------------
type decl struct {
// Name starts with '$' if the declaration describes an anonymous type.
// '$s_%d' for anonymous struct types
// '$i_%d' for anonymous interface types
name string
typ ast.Expr
class decl_class
flags decl_flags
// functions for interface type, fields+methods for struct type
children map[string]*decl
// embedded types
embedded []ast.Expr
// if the type is unknown at AST building time, I'm using these
value ast.Expr
// if it's a multiassignment and the Value is a CallExpr, it is being set
// to an index into the return value tuple, otherwise it's a -1
value_index int
// scope where this Decl was declared in (not its visibilty scope!)
// Decl uses it for type inference
scope *scope
}
func ast_decl_type(d ast.Decl) ast.Expr {
switch t := d.(type) {
case *ast.GenDecl:
switch t.Tok {
case token.CONST, token.VAR:
c := t.Specs[0].(*ast.ValueSpec)
return c.Type
case token.TYPE:
t := t.Specs[0].(*ast.TypeSpec)
return t.Type
}
case *ast.FuncDecl:
return t.Type
}
panic("unreachable")
return nil
}
func ast_decl_class(d ast.Decl) decl_class {
switch t := d.(type) {
case *ast.GenDecl:
switch t.Tok {
case token.VAR:
return decl_var
case token.CONST:
return decl_const
case token.TYPE:
return decl_type
}
case *ast.FuncDecl:
return decl_func
}
panic("unreachable")
}
func ast_decl_convertable(d ast.Decl) bool {
switch t := d.(type) {
case *ast.GenDecl:
switch t.Tok {
case token.VAR, token.CONST, token.TYPE:
return true
}
case *ast.FuncDecl:
return true
}
return false
}
func ast_field_list_to_decls(f *ast.FieldList, class decl_class, flags decl_flags, scope *scope) map[string]*decl {
count := 0
for _, field := range f.List {
count += len(field.Names)
}
decls := make(map[string]*decl, count)
for _, field := range f.List {
for _, name := range field.Names {
if flags&decl_foreign != 0 && !ast.IsExported(name.Name) {
continue
}
d := &decl{
name: name.Name,
typ: field.Type,
class: class,
flags: flags,
scope: scope,
value_index: -1,
}
decls[d.name] = d
}
// add anonymous field as a child (type embedding)
if class == decl_var && field.Names == nil {
tp := get_type_path(field.Type)
if flags&decl_foreign != 0 && !ast.IsExported(tp.name) {
continue
}
d := &decl{
name: tp.name,
typ: field.Type,
class: class,
flags: flags,
scope: scope,
value_index: -1,
}
decls[d.name] = d
}
}
return decls
}
func ast_field_list_to_embedded(f *ast.FieldList) []ast.Expr {
count := 0
for _, field := range f.List {
if field.Names == nil || field.Names[0].Name == "?" {
count++
}
}
if count == 0 {
return nil
}
embedded := make([]ast.Expr, count)
i := 0
for _, field := range f.List {
if field.Names == nil || field.Names[0].Name == "?" {
embedded[i] = field.Type
i++
}
}
return embedded
}
func ast_type_to_embedded(ty ast.Expr) []ast.Expr {
switch t := ty.(type) {
case *ast.StructType:
return ast_field_list_to_embedded(t.Fields)
case *ast.InterfaceType:
return ast_field_list_to_embedded(t.Methods)
}
return nil
}
func ast_type_to_children(ty ast.Expr, flags decl_flags, scope *scope) map[string]*decl {
switch t := ty.(type) {
case *ast.StructType:
return ast_field_list_to_decls(t.Fields, decl_var, flags, scope)
case *ast.InterfaceType:
return ast_field_list_to_decls(t.Methods, decl_func, flags, scope)
}
return nil
}
//-------------------------------------------------------------------------
// anonymous_id_gen
//
// ID generator for anonymous types (thread-safe)
//-------------------------------------------------------------------------
type anonymous_id_gen struct {
sync.Mutex
i int
}
func (a *anonymous_id_gen) gen() (id int) {
a.Lock()
defer a.Unlock()
id = a.i
a.i++
return
}
var g_anon_gen anonymous_id_gen
//-------------------------------------------------------------------------
func check_for_anon_type(t ast.Expr, flags decl_flags, s *scope) ast.Expr {
if t == nil {
return nil
}
var name string
switch t.(type) {
case *ast.StructType:
name = fmt.Sprintf("$s_%d", g_anon_gen.gen())
case *ast.InterfaceType:
name = fmt.Sprintf("$i_%d", g_anon_gen.gen())
}
if name != "" {
anonymify_ast(t, flags, s)
d := new_decl_full(name, decl_type, flags, t, nil, -1, s)
s.add_named_decl(d)
return ast.NewIdent(name)
}
return t
}
//-------------------------------------------------------------------------
func new_decl_full(name string, class decl_class, flags decl_flags, typ, v ast.Expr, vi int, s *scope) *decl {
d := new(decl)
d.name = name
d.class = class
d.flags = flags
d.typ = typ
d.value = v
d.value_index = vi
d.scope = s
d.children = ast_type_to_children(d.typ, flags, s)
d.embedded = ast_type_to_embedded(d.typ)
return d
}
func new_decl(name string, class decl_class, scope *scope) *decl {
decl := new(decl)
decl.name = name
decl.class = class
decl.value_index = -1
decl.scope = scope
return decl
}
func new_decl_var(name string, typ ast.Expr, value ast.Expr, vindex int, scope *scope) *decl {
if name == "_" {
return nil
}
decl := new(decl)
decl.name = name
decl.class = decl_var
decl.typ = typ
decl.value = value
decl.value_index = vindex
decl.scope = scope
return decl
}
func method_of(d ast.Decl) string {
if t, ok := d.(*ast.FuncDecl); ok {
if t.Recv != nil {
switch t := t.Recv.List[0].Type.(type) {
case *ast.StarExpr:
return t.X.(*ast.Ident).Name
case *ast.Ident:
return t.Name
default:
return ""
}
}
}
return ""
}
func (other *decl) deep_copy() *decl {
d := new(decl)
d.name = other.name
d.class = other.class
d.typ = other.typ
d.value = other.value
d.value_index = other.value_index
d.children = make(map[string]*decl, len(other.children))
for key, value := range other.children {
d.children[key] = value
}
if other.embedded != nil {
d.embedded = make([]ast.Expr, len(other.embedded))
copy(d.embedded, other.embedded)
}
d.scope = other.scope
return d
}
func (d *decl) clear_visited() {
d.flags &^= decl_visited
}
func (d *decl) expand_or_replace(other *decl) {
// expand only if it's a methods stub, otherwise simply copy
if d.class != decl_methods_stub && other.class != decl_methods_stub {
d = other
return
}
if d.class == decl_methods_stub {
d.typ = other.typ
d.class = other.class
}
if other.children != nil {
for _, c := range other.children {
d.add_child(c)
}
}
if other.embedded != nil {
d.embedded = other.embedded
d.scope = other.scope
}
}
func (d *decl) matches() bool {
if strings.HasPrefix(d.name, "$") || d.class == decl_methods_stub {
return false
}
return true
}
func (d *decl) pretty_print_type(out io.Writer) {
switch d.class {
case decl_type:
switch d.typ.(type) {
case *ast.StructType:
// TODO: not used due to anonymify?
fmt.Fprintf(out, "struct")
case *ast.InterfaceType:
// TODO: not used due to anonymify?
fmt.Fprintf(out, "interface")
default:
if d.typ != nil {
pretty_print_type_expr(out, d.typ)
}
}
case decl_var:
if d.typ != nil {
pretty_print_type_expr(out, d.typ)
}
case decl_func:
pretty_print_type_expr(out, d.typ)
}
}
func (d *decl) add_child(cd *decl) {
if d.children == nil {
d.children = make(map[string]*decl)
}
d.children[cd.name] = cd
}
func check_for_builtin_funcs(typ *ast.Ident, c *ast.CallExpr, scope *scope) (ast.Expr, *scope) {
if strings.HasPrefix(typ.Name, "func(") {
if t, ok := c.Fun.(*ast.Ident); ok {
switch t.Name {
case "new":
if len(c.Args) > 0 {
e := new(ast.StarExpr)
e.X = c.Args[0]
return e, scope
}
case "make":
if len(c.Args) > 0 {
return c.Args[0], scope
}
case "append":
if len(c.Args) > 0 {
t, scope, _ := infer_type(c.Args[0], scope, -1)
return t, scope
}
case "complex":
// TODO: fix it
return ast.NewIdent("complex"), g_universe_scope
case "closed":
return ast.NewIdent("bool"), g_universe_scope
case "cap":
return ast.NewIdent("int"), g_universe_scope
case "copy":
return ast.NewIdent("int"), g_universe_scope
case "len":
return ast.NewIdent("int"), g_universe_scope
}
// TODO:
// func recover() interface{}
// func imag(c ComplexType) FloatType
// func real(c ComplexType) FloatType
}
}
return nil, nil
}
func func_return_type(f *ast.FuncType, index int) ast.Expr {
if f.Results == nil {
return nil
}
if index == -1 {
return f.Results.List[0].Type
}
i := 0
var field *ast.Field
for _, field = range f.Results.List {
if i >= index {
return field.Type
}
if field.Names != nil {
i += len(field.Names)
} else {
i++
}
}
if i >= index {
return field.Type
}
return nil
}
type type_path struct {
pkg string
name string
}
func (tp *type_path) is_nil() bool {
return tp.pkg == "" && tp.name == ""
}
// converts type expressions like:
// ast.Expr
// *ast.Expr
// $ast$go/ast.Expr
// to a path that can be used to lookup a type related Decl
func get_type_path(e ast.Expr) (r type_path) {
if e == nil {
return type_path{"", ""}
}
switch t := e.(type) {
case *ast.Ident:
r.name = t.Name
case *ast.StarExpr:
r = get_type_path(t.X)
case *ast.SelectorExpr:
if ident, ok := t.X.(*ast.Ident); ok {
r.pkg = ident.Name
}
r.name = t.Sel.Name
}
return
}
func lookup_path(tp type_path, scope *scope) *decl {
if tp.is_nil() {
return nil
}
var decl *decl
if tp.pkg != "" {
decl = scope.lookup(tp.pkg)
// return nil early if the package wasn't found but it's part
// of the type specification
if decl == nil {
return nil
}
}
if decl != nil {
if tp.name != "" {
return decl.find_child(tp.name)
} else {
return decl
}
}
return scope.lookup(tp.name)
}
func lookup_pkg(tp type_path, scope *scope) string {
if tp.is_nil() {
return ""
}
if tp.pkg == "" {
return ""
}
decl := scope.lookup(tp.pkg)
if decl == nil {
return ""
}
return decl.name
}
func type_to_decl(t ast.Expr, scope *scope) *decl {
tp := get_type_path(t)
return lookup_path(tp, scope)
}
func expr_to_decl(e ast.Expr, scope *scope) *decl {
t, scope, _ := infer_type(e, scope, -1)
return type_to_decl(t, scope)
}
//-------------------------------------------------------------------------
// Type inference
//-------------------------------------------------------------------------
type type_predicate func(ast.Expr) bool
func advance_to_type(pred type_predicate, v ast.Expr, scope *scope) (ast.Expr, *scope) {
if pred(v) {
return v, scope
}
for {
decl := type_to_decl(v, scope)
if decl == nil {
return nil, nil
}
v = decl.typ
scope = decl.scope
if pred(v) {
break
}
}
return v, scope
}
func advance_to_struct_or_interface(decl *decl) *decl {
if struct_interface_predicate(decl.typ) {
return decl
}
for {
decl = type_to_decl(decl.typ, decl.scope)
if decl == nil {
return nil
}
if struct_interface_predicate(decl.typ) {
break
}
}
return decl
}
func struct_interface_predicate(v ast.Expr) bool {
switch v.(type) {
case *ast.StructType, *ast.InterfaceType:
return true
}
return false
}
func chan_predicate(v ast.Expr) bool {
_, ok := v.(*ast.ChanType)
return ok
}
func index_predicate(v ast.Expr) bool {
switch v.(type) {
case *ast.ArrayType, *ast.MapType, *ast.Ellipsis:
return true
}
return false
}
func star_predicate(v ast.Expr) bool {
_, ok := v.(*ast.StarExpr)
return ok
}
func func_predicate(v ast.Expr) bool {
_, ok := v.(*ast.FuncType)
return ok
}
func range_predicate(v ast.Expr) bool {
switch t := v.(type) {
case *ast.Ident:
if t.Name == "string" {
return true
}
case *ast.ArrayType, *ast.MapType, *ast.ChanType, *ast.Ellipsis:
return true
}
return false
}
type anonymous_typer struct {
flags decl_flags
scope *scope
}
func (a *anonymous_typer) Visit(node ast.Node) ast.Visitor {
switch t := node.(type) {
case *ast.CompositeLit:
t.Type = check_for_anon_type(t.Type, a.flags, a.scope)
case *ast.MapType:
t.Key = check_for_anon_type(t.Key, a.flags, a.scope)
t.Value = check_for_anon_type(t.Value, a.flags, a.scope)
case *ast.ArrayType:
t.Elt = check_for_anon_type(t.Elt, a.flags, a.scope)
case *ast.Ellipsis:
t.Elt = check_for_anon_type(t.Elt, a.flags, a.scope)
case *ast.ChanType:
t.Value = check_for_anon_type(t.Value, a.flags, a.scope)
case *ast.Field:
t.Type = check_for_anon_type(t.Type, a.flags, a.scope)
case *ast.CallExpr:
t.Fun = check_for_anon_type(t.Fun, a.flags, a.scope)
case *ast.ParenExpr:
t.X = check_for_anon_type(t.X, a.flags, a.scope)
case *ast.GenDecl:
switch t.Tok {
case token.VAR:
for _, s := range t.Specs {
vs := s.(*ast.ValueSpec)
vs.Type = check_for_anon_type(vs.Type, a.flags, a.scope)
}
}
}
return a
}
func anonymify_ast(node ast.Node, flags decl_flags, scope *scope) {
v := anonymous_typer{flags, scope}
ast.Walk(&v, node)
}
// RETURNS:
// - type expression which represents a full name of a type
// - bool whether a type expression is actually a type (used internally)
// - scope in which type makes sense
func infer_type(v ast.Expr, scope *scope, index int) (ast.Expr, *scope, bool) {
switch t := v.(type) {
case *ast.CompositeLit:
return t.Type, scope, true
case *ast.Ident:
if d := scope.lookup(t.Name); d != nil {
if d.class == decl_package {
return ast.NewIdent(t.Name), scope, false
}
typ, scope := d.infer_type()
return typ, scope, d.class == decl_type
}
case *ast.UnaryExpr:
switch t.Op {
case token.AND:
// &a makes sense only with values, don't even check for type
it, s, _ := infer_type(t.X, scope, -1)
if it == nil {
break
}
e := new(ast.StarExpr)
e.X = it
return e, s, false
case token.ARROW:
// <-a makes sense only with values
it, s, _ := infer_type(t.X, scope, -1)
if it == nil {
break
}
switch index {
case -1, 0:
it, s = advance_to_type(chan_predicate, it, s)
return it.(*ast.ChanType).Value, s, false
case 1:
// technically it's a value, but in case of index == 1
// it is always the last infer operation
return ast.NewIdent("bool"), g_universe_scope, false
}
case token.ADD, token.NOT, token.SUB, token.XOR:
it, s, _ := infer_type(t.X, scope, -1)
if it == nil {
break
}
return it, s, false
}
case *ast.BinaryExpr:
switch t.Op {
case token.EQL, token.NEQ, token.LSS, token.LEQ,
token.GTR, token.GEQ, token.LOR, token.LAND:
// logic operations, the result is a bool, always
return ast.NewIdent("bool"), g_universe_scope, false
case token.ADD, token.SUB, token.MUL, token.QUO, token.OR,
token.XOR, token.REM, token.AND, token.AND_NOT:
// try X, then Y, they should be the same anyway
it, s, _ := infer_type(t.X, scope, -1)
if it == nil {
it, s, _ = infer_type(t.Y, scope, -1)
if it == nil {
break
}
}
return it, s, false
case token.SHL, token.SHR:
// try only X for shifts, Y is always uint
it, s, _ := infer_type(t.X, scope, -1)
if it == nil {
break
}
return it, s, false
}
case *ast.IndexExpr:
// something[another] always returns a value and it works on a value too
it, s, _ := infer_type(t.X, scope, -1)
if it == nil {
break
}
it, s = advance_to_type(index_predicate, it, s)
switch t := it.(type) {
case *ast.ArrayType:
return t.Elt, s, false
case *ast.Ellipsis:
return t.Elt, s, false
case *ast.MapType:
switch index {
case -1, 0:
return t.Value, s, false
case 1:
return ast.NewIdent("bool"), g_universe_scope, false
}
}
case *ast.SliceExpr:
// something[start : end] always returns a value
it, s, _ := infer_type(t.X, scope, -1)
if it == nil {
break
}
it, s = advance_to_type(index_predicate, it, s)
switch t := it.(type) {
case *ast.ArrayType:
e := new(ast.ArrayType)
e.Elt = t.Elt
return e, s, false
}
case *ast.StarExpr:
it, s, is_type := infer_type(t.X, scope, -1)
if it == nil {
break
}
if is_type {
// if it's a type, add * modifier, make it a 'pointer of' type
e := new(ast.StarExpr)
e.X = it
return e, s, true
} else {
it, s := advance_to_type(star_predicate, it, s)
if se, ok := it.(*ast.StarExpr); ok {
return se.X, s, false
}
}
case *ast.CallExpr:
// this is a function call or a type cast:
// myFunc(1,2,3) or int16(myvar)
it, s, is_type := infer_type(t.Fun, scope, -1)
if it == nil {
break
}
if is_type {
// a type cast
return it, scope, false
} else {
// it must be a function call or a built-in function
// first check for built-in
if ct, ok := it.(*ast.Ident); ok {
ty, s := check_for_builtin_funcs(ct, t, scope)
if ty != nil {
return ty, s, false
}
}
// then check for an ordinary function call
it, scope = advance_to_type(func_predicate, it, s)
if ct, ok := it.(*ast.FuncType); ok {
return func_return_type(ct, index), s, false
}
}
case *ast.ParenExpr:
it, s, is_type := infer_type(t.X, scope, -1)
if it == nil {
break
}
return it, s, is_type
case *ast.SelectorExpr:
it, s, _ := infer_type(t.X, scope, -1)
if it == nil {
break
}
if d := type_to_decl(it, s); d != nil {
c := d.find_child_and_in_embedded(t.Sel.Name)
if c != nil {
if c.class == decl_type {
return t, scope, true
} else {
typ, s := c.infer_type()
return typ, s, false
}
}
}
case *ast.FuncLit:
// it's a value, but I think most likely we don't even care, cause we can only
// call it, and CallExpr uses the type itself to figure out
return t.Type, scope, false
case *ast.TypeAssertExpr:
if t.Type == nil {
return infer_type(t.X, scope, -1)
}
switch index {
case -1, 0:
// converting a value to a different type, but return thing is a value
it, _, _ := infer_type(t.Type, scope, -1)
return it, scope, false
case 1:
return ast.NewIdent("bool"), g_universe_scope, false
}
case *ast.ArrayType, *ast.MapType, *ast.ChanType, *ast.Ellipsis,
*ast.FuncType, *ast.StructType, *ast.InterfaceType:
return t, scope, true
default:
_ = reflect.TypeOf(v)
//fmt.Println(ty)
}
return nil, nil, false
}
// Uses Value, ValueIndex and Scope to infer the type of this
// declaration. Returns the type itself and the scope where this type
// makes sense.
func (d *decl) infer_type() (ast.Expr, *scope) {
// special case for range vars
if d.flags&decl_rangevar != 0 {
var scope *scope
d.typ, scope = infer_range_type(d.value, d.scope, d.value_index)
return d.typ, scope
}
switch d.class {
case decl_package:
// package is handled specially in inferType
return nil, nil
case decl_type:
return ast.NewIdent(d.name), d.scope
}
// shortcut
if d.typ != nil && d.value == nil {
return d.typ, d.scope
}
// prevent loops
if d.flags&decl_visited != 0 {
return nil, nil
}
d.flags |= decl_visited
defer d.clear_visited()
var scope *scope
d.typ, scope, _ = infer_type(d.value, d.scope, d.value_index)
return d.typ, scope
}
func (d *decl) find_child(name string) *decl {
if d.children != nil {
if c, ok := d.children[name]; ok {
return c
}
}
decl := advance_to_struct_or_interface(d)
if decl != nil && decl != d {
return decl.find_child(name)
}
return nil
}
func (d *decl) find_child_and_in_embedded(name string) *decl {
c := d.find_child(name)
if c == nil {
for _, e := range d.embedded {
typedecl := type_to_decl(e, d.scope)
c = typedecl.find_child_and_in_embedded(name)
if c != nil {
break
}
}
}
return c
}
// Special type inference for range statements.
// [int], [int] := range [string]
// [int], [value] := range [slice or array]
// [key], [value] := range [map]
// [value], [nil] := range [chan]
func infer_range_type(e ast.Expr, sc *scope, valueindex int) (ast.Expr, *scope) {
t, s, _ := infer_type(e, sc, -1)
t, s = advance_to_type(range_predicate, t, s)
if t != nil {
var t1, t2 ast.Expr
var s1, s2 *scope
s1 = s
s2 = s
switch t := t.(type) {
case *ast.Ident:
// string
if t.Name == "string" {
t1 = ast.NewIdent("int")
t2 = ast.NewIdent("rune")
s1 = g_universe_scope
s2 = g_universe_scope
} else {
t1, t2 = nil, nil
}
case *ast.ArrayType:
t1 = ast.NewIdent("int")
s1 = g_universe_scope
t2 = t.Elt
case *ast.Ellipsis:
t1 = ast.NewIdent("int")
s1 = g_universe_scope
t2 = t.Elt
case *ast.MapType:
t1 = t.Key
t2 = t.Value
case *ast.ChanType:
t1 = t.Value
t2 = nil
default: