forked from nsf/gocode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decl.go
1332 lines (1204 loc) · 29.9 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
const (
DECL_CONST = iota
DECL_VAR
DECL_TYPE
DECL_FUNC
DECL_PACKAGE
// this one serves as a temporary type for those methods that were
// declared before their actual owner
DECL_METHODS_STUB
)
// Decl.Flags
const (
DECL_FOREIGN = 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
)
var declClassToString = [...]string{
DECL_CONST: "const",
DECL_VAR: "var",
DECL_TYPE: "type",
DECL_FUNC: "func",
DECL_PACKAGE: "package",
DECL_METHODS_STUB: "IF YOU SEE THIS, REPORT A BUG", // :D
}
//-------------------------------------------------------------------------
// 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
Type ast.Expr
Class int16
Flags int16
// 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
ValueIndex int
// scope where this Decl was declared in (not its visibilty scope!)
// Decl uses it for type inference
Scope *Scope
}
func astDeclType(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 astDeclClass(d ast.Decl) int {
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")
return 0
}
func astDeclConvertable(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 astFieldListToDecls(f *ast.FieldList, class int, flags int, scope *Scope) map[string]*Decl {
count := 0
for _, field := range f.List {
count += len(field.Names)
}
if count == 0 {
return nil
}
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,
Type: field.Type,
Class: int16(class),
Flags: int16(flags),
Scope: scope,
ValueIndex: -1,
}
decls[d.Name] = d
}
// add anonymous field as a child (type embedding)
if class == DECL_VAR && field.Names == nil {
tp := typePath(field.Type)
if flags&DECL_FOREIGN != 0 && !ast.IsExported(tp.name) {
continue
}
d := &Decl{
Name: tp.name,
Type: field.Type,
Class: int16(class),
Flags: int16(flags),
Scope: scope,
ValueIndex: -1,
}
decls[d.Name] = d
}
}
return decls
}
func astFieldListToEmbedded(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 astTypeToEmbedded(ty ast.Expr) []ast.Expr {
switch t := ty.(type) {
case *ast.StructType:
return astFieldListToEmbedded(t.Fields)
case *ast.InterfaceType:
return astFieldListToEmbedded(t.Methods)
}
return nil
}
func astTypeToChildren(ty ast.Expr, flags int, scope *Scope) map[string]*Decl {
switch t := ty.(type) {
case *ast.StructType:
return astFieldListToDecls(t.Fields, DECL_VAR, flags, scope)
case *ast.InterfaceType:
return astFieldListToDecls(t.Methods, DECL_FUNC, flags, scope)
}
return nil
}
//-------------------------------------------------------------------------
// AnonymousIDGen
// ID generator for anonymous types (thread-safe)
//-------------------------------------------------------------------------
type AnonymousIDGen struct {
sync.Mutex
i int
}
func (a *AnonymousIDGen) Gen() (id int) {
a.Lock()
defer a.Unlock()
id = a.i
a.i++
return
}
var anonGen AnonymousIDGen
//-------------------------------------------------------------------------
func checkForAnonType(t ast.Expr, flags int, s *Scope) ast.Expr {
if t == nil {
return nil
}
var name string
switch t.(type) {
case *ast.StructType:
name = fmt.Sprintf("$s_%d", anonGen.Gen())
case *ast.InterfaceType:
name = fmt.Sprintf("$i_%d", anonGen.Gen())
}
if name != "" {
anonymifyAst(t, flags, s)
d := NewDeclAnonType(name, flags, t, s)
s.addNamedDecl(d)
return ast.NewIdent(name)
}
return t
}
//-------------------------------------------------------------------------
func NewDecl2(name string, class, flags int, typ, v ast.Expr, vi int, s *Scope) *Decl {
d := new(Decl)
d.Name = name
d.Class = int16(class)
d.Flags = int16(flags)
d.Type = typ
d.Value = v
d.ValueIndex = vi
d.Scope = s
d.Children = astTypeToChildren(d.Type, flags, s)
d.Embedded = astTypeToEmbedded(d.Type)
return d
}
func NewDeclAnonType(name string, flags int, typ ast.Expr, s *Scope) *Decl {
d := NewDecl(name, DECL_TYPE, s)
d.Type = typ
d.Flags = int16(flags)
d.Children = astTypeToChildren(d.Type, flags, s)
d.Embedded = astTypeToEmbedded(d.Type)
return d
}
func NewDeclTyped(name string, class int, typ ast.Expr, scope *Scope) *Decl {
d := NewDecl(name, class, scope)
d.Type = typ
return d
}
func NewDeclTypedNamed(name string, class int, typ string, scope *Scope) *Decl {
d := NewDecl(name, class, scope)
d.Type = ast.NewIdent(typ)
return d
}
func NewDecl(name string, class int, scope *Scope) *Decl {
decl := new(Decl)
decl.Name = name
decl.Class = int16(class)
decl.ValueIndex = -1
decl.Scope = scope
return decl
}
func NewDeclVar(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.Type = typ
decl.Value = value
decl.ValueIndex = vindex
decl.Scope = scope
return decl
}
func NewDeclBuiltinError(scope *Scope) *Decl {
d := NewDecl("error", DECL_TYPE, scope)
d.Type = &ast.InterfaceType{}
d.Children = make(map[string]*Decl)
d.Children["Error"] = NewDeclTyped(
"Error",
DECL_FUNC,
&ast.FuncType{
Results: &ast.FieldList{
List: []*ast.Field{
{
Type: ast.NewIdent("string"),
},
},
},
},
scope,
)
return d
}
func MethodOf(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 ""
}
// complete copy
func (d *Decl) Copy(other *Decl) {
d.Name = other.Name
d.Class = other.Class
d.Type = other.Type
d.Value = other.Value
d.ValueIndex = other.ValueIndex
d.Children = other.Children
d.Embedded = other.Embedded
d.Scope = other.Scope
}
func (other *Decl) DeepCopy() *Decl {
d := new(Decl)
d.Name = other.Name
d.Class = other.Class
d.Type = other.Type
d.Value = other.Value
d.ValueIndex = other.ValueIndex
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) ClassName() string {
return declClassToString[d.Class]
}
func (d *Decl) ExpandOrReplace(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.Copy(other)
return
}
if d.Class == DECL_METHODS_STUB {
d.Type = other.Type
d.Class = other.Class
}
if other.Children != nil {
for _, c := range other.Children {
d.AddChild(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) PrettyPrintType(out io.Writer) {
switch d.Class {
case DECL_TYPE:
switch d.Type.(type) {
case *ast.StructType:
fmt.Fprintf(out, "struct")
case *ast.InterfaceType:
fmt.Fprintf(out, "interface")
default:
if d.Type != nil {
prettyPrintTypeExpr(out, d.Type)
}
}
case DECL_VAR:
if d.Type != nil {
prettyPrintTypeExpr(out, d.Type)
}
case DECL_FUNC:
prettyPrintTypeExpr(out, d.Type)
}
}
func (d *Decl) AddChild(cd *Decl) {
if d.Children == nil {
d.Children = make(map[string]*Decl)
}
d.Children[cd.Name] = cd
}
func checkForBuiltinFuncs(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":
e := new(ast.StarExpr)
e.X = c.Args[0]
return e, scope
case "make":
return c.Args[0], scope
case "append":
return c.Args[0], scope
case "cmplx":
return ast.NewIdent("complex"), universeScope
case "closed":
return ast.NewIdent("bool"), universeScope
}
}
}
return nil, nil
}
func funcReturnType(f *ast.FuncType, index int) ast.Expr {
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 TypePath struct {
pkg string
name string
}
func (tp *TypePath) IsNil() 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 typePath(e ast.Expr) (r TypePath) {
if e == nil {
return TypePath{"", ""}
}
switch t := e.(type) {
case *ast.Ident:
r.name = t.Name
case *ast.StarExpr:
r = typePath(t.X)
case *ast.SelectorExpr:
if ident, ok := t.X.(*ast.Ident); ok {
r.pkg = ident.Name
}
r.name = t.Sel.Name
}
return
}
func lookupPath(tp TypePath, scope *Scope) *Decl {
if tp.IsNil() {
return nil
}
var decl *Decl
if tp.pkg != "" {
decl = scope.lookup(tp.pkg)
}
if decl != nil {
if tp.name != "" {
return decl.FindChild(tp.name)
} else {
return decl
}
}
return scope.lookup(tp.name)
}
func typeToDecl(t ast.Expr, scope *Scope) *Decl {
tp := typePath(t)
return lookupPath(tp, scope)
}
func exprToDecl(e ast.Expr, scope *Scope) *Decl {
t, scope, _ := inferType(e, scope, -1)
return typeToDecl(t, scope)
}
//-------------------------------------------------------------------------
// Type inference
//-------------------------------------------------------------------------
type TypePredicate func(ast.Expr) bool
func advanceToType(pred TypePredicate, v ast.Expr, scope *Scope) (ast.Expr, *Scope) {
if pred(v) {
return v, scope
}
for {
decl := typeToDecl(v, scope)
if decl == nil {
return nil, nil
}
v = decl.Type
scope = decl.Scope
if pred(v) {
break
}
}
return v, scope
}
func advanceToStructOrInterface(decl *Decl) *Decl {
if structInterfacePredicate(decl.Type) {
return decl
}
for {
decl = typeToDecl(decl.Type, decl.Scope)
if decl == nil {
return nil
}
if structInterfacePredicate(decl.Type) {
break
}
}
return decl
}
func structInterfacePredicate(v ast.Expr) bool {
switch v.(type) {
case *ast.StructType, *ast.InterfaceType:
return true
}
return false
}
func chanPredicate(v ast.Expr) bool {
_, ok := v.(*ast.ChanType)
return ok
}
func indexPredicate(v ast.Expr) bool {
switch v.(type) {
case *ast.ArrayType, *ast.MapType, *ast.Ellipsis:
return true
}
return false
}
func starPredicate(v ast.Expr) bool {
_, ok := v.(*ast.StarExpr)
return ok
}
func funcPredicate(v ast.Expr) bool {
_, ok := v.(*ast.FuncType)
return ok
}
func rangePredicate(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 anonymousTyper struct {
flags int
scope *Scope
}
func (a *anonymousTyper) Visit(node ast.Node) ast.Visitor {
switch t := node.(type) {
case *ast.CompositeLit:
t.Type = checkForAnonType(t.Type, a.flags, a.scope)
case *ast.MapType:
t.Key = checkForAnonType(t.Key, a.flags, a.scope)
t.Value = checkForAnonType(t.Value, a.flags, a.scope)
case *ast.ArrayType:
t.Elt = checkForAnonType(t.Elt, a.flags, a.scope)
case *ast.Ellipsis:
t.Elt = checkForAnonType(t.Elt, a.flags, a.scope)
case *ast.ChanType:
t.Value = checkForAnonType(t.Value, a.flags, a.scope)
case *ast.Field:
t.Type = checkForAnonType(t.Type, a.flags, a.scope)
case *ast.CallExpr:
t.Fun = checkForAnonType(t.Fun, a.flags, a.scope)
case *ast.ParenExpr:
t.X = checkForAnonType(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 = checkForAnonType(vs.Type, a.flags, a.scope)
}
}
}
return a
}
func anonymifyAst(node ast.Node, flags int, scope *Scope) {
v := anonymousTyper{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 inferType(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.InferType()
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, _ := inferType(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, _ := inferType(t.X, scope, -1)
if it == nil {
break
}
switch index {
case -1, 0:
it, s = advanceToType(chanPredicate, 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"), universeScope, false
}
case token.ADD, token.NOT, token.SUB, token.XOR:
it, s, _ := inferType(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"), universeScope, 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, _ := inferType(t.X, scope, -1)
if it == nil {
it, s, _ = inferType(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, _ := inferType(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, _ := inferType(t.X, scope, -1)
if it == nil {
break
}
it, s = advanceToType(indexPredicate, 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"), universeScope, false
}
}
case *ast.SliceExpr:
// something[start : end] always returns a value
it, s, _ := inferType(t.X, scope, -1)
if it == nil {
break
}
it, s = advanceToType(indexPredicate, 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, isType := inferType(t.X, scope, -1)
if it == nil {
break
}
if isType {
// 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 := advanceToType(starPredicate, 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, isType := inferType(t.Fun, scope, -1)
if it == nil {
break
}
if isType {
// 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 := checkForBuiltinFuncs(ct, t, scope)
if ty != nil {
return ty, s, false
}
}
// then check for an ordinary function call
it, scope = advanceToType(funcPredicate, it, s)
if ct, ok := it.(*ast.FuncType); ok {
return funcReturnType(ct, index), s, false
}
}
case *ast.ParenExpr:
it, s, isType := inferType(t.X, scope, -1)
if it == nil {
break
}
return it, s, isType
case *ast.SelectorExpr:
it, s, _ := inferType(t.X, scope, -1)
if it == nil {
break
}
if d := typeToDecl(it, s); d != nil {
c := d.FindChildAndInEmbedded(t.Sel.Name)
if c != nil {
if c.Class == DECL_TYPE {
return t, scope, true
} else {
typ, s := c.InferType()
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 inferType(t.X, scope, -1)
}
switch index {
case -1, 0:
// converting a value to a different type, but return thing is a value
it, _, _ := inferType(t.Type, scope, -1)
return it, scope, false
case 1:
return ast.NewIdent("bool"), universeScope, 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) InferType() (ast.Expr, *Scope) {
// special case for range vars
if d.Flags&DECL_RANGEVAR != 0 {
var scope *Scope
d.Type, scope = inferRangeType(d.Value, d.Scope, d.ValueIndex)
return d.Type, 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.Type != nil && d.Value == nil {
return d.Type, d.Scope
}
var scope *Scope
d.Type, scope, _ = inferType(d.Value, d.Scope, d.ValueIndex)
return d.Type, scope
}
func (d *Decl) FindChild(name string) *Decl {
if d.Children != nil {
if c, ok := d.Children[name]; ok {
return c
}
}
decl := advanceToStructOrInterface(d)
if decl != nil && decl != d {
return decl.FindChild(name)
}
return nil
}
func (d *Decl) FindChildAndInEmbedded(name string) *Decl {
c := d.FindChild(name)
if c == nil {
for _, e := range d.Embedded {
typedecl := typeToDecl(e, d.Scope)
c = typedecl.FindChildAndInEmbedded(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 inferRangeType(e ast.Expr, scope *Scope, valueindex int) (ast.Expr, *Scope) {
t, s, _ := inferType(e, scope, -1)
t, s = advanceToType(rangePredicate, 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 = universeScope
s2 = universeScope
} else {
t1, t2 = nil, nil
}
case *ast.ArrayType:
t1 = ast.NewIdent("int")
s1 = universeScope
t2 = t.Elt
case *ast.Ellipsis:
t1 = ast.NewIdent("int")
s1 = universeScope
t2 = t.Elt
case *ast.MapType:
t1 = t.Key
t2 = t.Value
case *ast.ChanType:
t1 = t.Value
t2 = nil
default:
t1, t2 = nil, nil
}
switch valueindex {
case 0:
return t1, s1
case 1:
return t2, s2
}
}
return nil, nil
}