-
Notifications
You must be signed in to change notification settings - Fork 1
/
decouple.go
1000 lines (883 loc) · 23.2 KB
/
decouple.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 decouple
import (
"fmt"
"go/ast"
"go/token"
"go/types"
"strings"
"github.com/bobg/errors"
"github.com/bobg/go-generics/v3/set"
"github.com/bobg/go-generics/v3/slices"
"golang.org/x/tools/go/packages"
)
// PkgMode is the minimal set of bit flags needed for the Config.Mode field of golang.org/x/go/packages
// for the result to be usable by a Checker.
const PkgMode = packages.NeedName | packages.NeedFiles | packages.NeedImports | packages.NeedDeps | packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo
// Checker is the object that can analyze a directory tree of Go code,
// or a set of packages loaded with "golang.org/x/go/packages".Load,
// or a single such package,
// or a function or function parameter in one.
//
// Set Verbose to true to get (very) verbose debugging output.
type Checker struct {
Verbose bool
pkgs []*packages.Package
namedInterfaces map[string]MethodMap // maps a package-qualified interface-type name to its method set
}
// NewCheckerFromDir creates a new Checker containing packages loaded
// (using "golang.org/x/go/packages".Load)
// from the given directory tree.
func NewCheckerFromDir(dir string) (Checker, error) {
conf := &packages.Config{Dir: dir, Mode: PkgMode}
pkgs, err := packages.Load(conf, "./...")
if err != nil {
return Checker{}, errors.Wrapf(err, "loading packages from %s", dir)
}
for _, pkg := range pkgs {
for _, pkgerr := range pkg.Errors {
err = errors.Join(err, errors.Wrapf(pkgerr, "in package %s", pkg.PkgPath))
}
}
if err != nil {
return Checker{}, errors.Wrapf(err, "after loading packages from %s", dir)
}
return NewCheckerFromPackages(pkgs), nil
}
// NewCheckerFromPackages creates a new Checker containing the given packages,
// which should be the result of calling "golang.org/x/go/packages".Load
// with at least the bits in PkgMode set in the Config.Mode field.
func NewCheckerFromPackages(pkgs []*packages.Package) Checker {
var (
namedInterfaces = make(map[string]MethodMap)
seen = set.New[*packages.Package]()
)
for _, pkg := range pkgs {
findNamedInterfaces(pkg, seen, namedInterfaces)
}
return Checker{pkgs: pkgs, namedInterfaces: namedInterfaces}
}
func findNamedInterfaces(pkg *packages.Package, seen set.Of[*packages.Package], namedInterfaces map[string]MethodMap) {
if seen.Has(pkg) {
return
}
seen.Add(pkg)
for _, ipkg := range pkg.Imports {
findNamedInterfaces(ipkg, seen, namedInterfaces)
}
if isInternal(pkg.PkgPath) {
return
}
for _, file := range pkg.Syntax {
for _, decl := range file.Decls {
gendecl, ok := decl.(*ast.GenDecl)
if !ok {
continue
}
if gendecl.Tok != token.TYPE {
continue
}
for _, spec := range gendecl.Specs {
typespec, ok := spec.(*ast.TypeSpec)
if !ok {
// Should be impossible.
continue
}
if !ast.IsExported(typespec.Name.Name) {
continue
}
obj := pkg.TypesInfo.Defs[typespec.Name]
if obj == nil {
// Should be impossible.
continue
}
intf := getType[*types.Interface](obj.Type())
if intf == nil {
continue
}
mm := make(MethodMap)
addMethodsToMap(intf, mm)
name := pkg.PkgPath
if strings.ContainsAny(name, "./") {
name = `"` + name + `"`
}
name += "." + typespec.Name.Name
namedInterfaces[name] = mm
}
}
}
}
// Check checks all the packages in the Checker.
// It analyzes the functions in them,
// looking for parameters with concrete types that could be interfaces instead.
// The result is a list of Tuples,
// one for each function checked that has parameters eligible for decoupling.
func (ch Checker) Check() ([]Tuple, error) {
var result []Tuple
for _, pkg := range ch.pkgs {
pkgResult, err := ch.CheckPackage(pkg)
if err != nil {
return nil, errors.Wrapf(err, "analyzing package %s", pkg.PkgPath)
}
result = append(result, pkgResult...)
}
return result, nil
}
// CheckPackage checks a single package.
// It should be one of the packages contained in the Checker.
// The result is a list of Tuples,
// one for each function checked that has parameters eligible for decoupling.
func (ch Checker) CheckPackage(pkg *packages.Package) ([]Tuple, error) {
var result []Tuple
for _, file := range pkg.Syntax {
for _, decl := range file.Decls {
fndecl, ok := decl.(*ast.FuncDecl)
if !ok {
continue
}
m, err := ch.CheckFunc(pkg, fndecl)
if err != nil {
return nil, errors.Wrapf(err, "analyzing function %s at %s", fndecl.Name.Name, pkg.Fset.Position(fndecl.Name.Pos()))
}
result = append(result, Tuple{
F: fndecl,
P: pkg,
M: m,
})
}
}
return result, nil
}
// Tuple is the type of a result from Checker.Check and Checker.CheckPackage.
type Tuple struct {
// F is the function declaration that this result is about.
F *ast.FuncDecl
// P is the package in which the function declaration appears.
P *packages.Package
// M is a map from the names of function parameters eligible for decoupling
// to MethodMaps for each such parameter.
M map[string]MethodMap
}
// Pos computes the filename and offset
// of the function name of the Tuple.
func (t Tuple) Pos() token.Position {
return t.P.Fset.Position(t.F.Name.Pos())
}
// MethodMap maps a set of method names to their calling signatures.
type MethodMap = map[string]*types.Signature
// CheckFunc checks a single function declaration,
// which should appear in the given package,
// which should be one of the packages contained in the Checker.
// The result is a map from parameter names eligible for decoupling to MethodMaps.
func (ch Checker) CheckFunc(pkg *packages.Package, fndecl *ast.FuncDecl) (map[string]MethodMap, error) {
result := make(map[string]MethodMap)
for _, field := range fndecl.Type.Params.List {
for _, name := range field.Names {
if name.Name == "_" {
continue
}
nameResult, err := ch.CheckParam(pkg, fndecl, name)
if err != nil {
return nil, errors.Wrapf(err, "analyzing parameter %s of %s", name.Name, fndecl.Name.Name)
}
if len(nameResult) != 0 {
result[name.Name] = nameResult
}
}
}
return result, nil
}
// CheckParam checks a single named parameter in a given function declaration,
// which must apepar in the given package,
// which should be one of the packages in the Checker.
// The result is a MethodMap for the parameter,
// and may be nil if the parameter is not eligible for decoupling.
func (ch Checker) CheckParam(pkg *packages.Package, fndecl *ast.FuncDecl, name *ast.Ident) (_ MethodMap, err error) {
defer func() {
if r := recover(); r != nil {
if e, ok := r.(error); ok {
var d derr
if errors.As(e, &d) {
err = d
return
}
}
panic(r)
}
}()
obj, ok := pkg.TypesInfo.Defs[name]
if !ok {
return nil, fmt.Errorf("no def found for %s", name.Name)
}
var (
intf = getType[*types.Interface](obj.Type())
mm MethodMap
)
if intf != nil {
mm = make(MethodMap)
addMethodsToMap(intf, mm)
}
a := analyzer{
name: name,
obj: obj,
pkg: pkg,
objmethods: mm,
methods: make(MethodMap),
enclosingFunc: &funcDeclOrLit{decl: fndecl},
debug: ch.Verbose,
}
a.debugf("fn %s param %s", fndecl.Name.Name, name.Name)
for _, stmt := range fndecl.Body.List {
if !a.stmt(stmt) {
return nil, nil
}
}
if len(a.objmethods) > 0 {
if len(a.methods) < len(a.objmethods) {
// A smaller interface will do.
return a.methods, nil
}
return nil, nil
}
return a.methods, nil
}
// NameForMethods takes a MethodMap
// and returns the name of an interface defining exactly the methods in it,
// if it can find one among the packages in the Checker.
// If there are multiple such interfaces,
// one is chosen arbitrarily.
func (ch Checker) NameForMethods(inp MethodMap) string {
for name, mm := range ch.namedInterfaces {
if sameMethodMaps(mm, inp) {
return name
}
}
return ""
}
type funcDeclOrLit struct {
decl *ast.FuncDecl
lit *ast.FuncLit
}
type analyzer struct {
name *ast.Ident
obj types.Object
pkg *packages.Package
// objmethods is input: the methodmap for obj's type,
// if that's an interface type.
// methods is output: the set of methods actually used.
objmethods, methods MethodMap
enclosingFunc *funcDeclOrLit
enclosingSwitchStmt *ast.SwitchStmt
level int
debug bool
}
func (a *analyzer) enclosingFuncInfo() (types.Type, token.Position, bool) {
if a.enclosingFunc == nil {
return nil, token.Position{}, false
}
if decl := a.enclosingFunc.decl; decl != nil {
obj, ok := a.pkg.TypesInfo.Defs[decl.Name]
if !ok {
return nil, token.Position{}, false
}
return obj.Type(), a.pos(obj), true
}
lit := a.enclosingFunc.lit
tv, ok := a.pkg.TypesInfo.Types[lit]
if !ok {
return nil, token.Position{}, false
}
return tv.Type, a.pos(lit), true
}
func (a *analyzer) getSig(expr ast.Expr) *types.Signature {
return getType[*types.Signature](a.pkg.TypesInfo.Types[expr].Type)
}
// Does expr denote the object in a?
func (a *analyzer) isObj(expr ast.Expr) bool {
switch expr := expr.(type) {
case *ast.Ident:
obj := a.pkg.TypesInfo.Uses[expr]
return obj == a.obj
case *ast.ParenExpr:
return a.isObj(expr.X)
default:
return false
}
}
func (a *analyzer) stmt(stmt ast.Stmt) (ok bool) {
a.level++
a.debugf("> stmt %#v", stmt)
defer func() {
a.debugf("< stmt %#v %v", stmt, ok)
a.level--
}()
if stmt == nil {
return true
}
switch stmt := stmt.(type) {
case *ast.AssignStmt:
for _, lhs := range stmt.Lhs {
// I think we can ignore the rhs value if a.isObj(lhs).
// What matters is only how our object is being used,
// not what's being assigned to it.
if !a.expr(lhs) {
return false
}
}
for i, rhs := range stmt.Rhs {
// xxx do a recursive analysis of how this var is used!
if a.isObj(rhs) && stmt.Tok != token.DEFINE {
if stmt.Tok != token.ASSIGN {
// Reject OP=
return false
}
tv, ok := a.pkg.TypesInfo.Types[stmt.Lhs[i]]
if !ok {
panic(errf("no type info for lvalue %d in assignment at %s", i, a.pos(stmt)))
}
intf := getType[*types.Interface](tv.Type)
if intf == nil {
return false
}
a.addMethods(intf)
continue
}
if !a.expr(rhs) {
return false
}
}
return true
case *ast.BlockStmt:
for _, s := range stmt.List {
if !a.stmt(s) {
return false
}
}
return true
case *ast.BranchStmt:
return true
case *ast.CaseClause:
for _, expr := range stmt.List {
if a.isObj(expr) {
if a.enclosingSwitchStmt == nil {
panic(errf("case clause with no enclosing switch statement at %s", a.pos(stmt)))
}
if a.enclosingSwitchStmt.Tag == nil {
return false // would require our obj to evaluate as a boolean
}
tv, ok := a.pkg.TypesInfo.Types[a.enclosingSwitchStmt.Tag]
if !ok {
panic(errf("no type info for switch tag at %s", a.pos(a.enclosingSwitchStmt.Tag)))
}
t1, t2 := a.obj.Type(), tv.Type
if !types.AssignableTo(t1, t2) && !types.AssignableTo(t2, t1) {
// "In any comparison, the first operand must be assignable to the type of the second operand, or vice versa."
// https://go.dev/ref/spec#Comparison_operators
return false
}
continue
}
if !a.expr(expr) {
return false
}
}
for _, s := range stmt.Body {
if !a.stmt(s) {
return false
}
}
return true
case *ast.CommClause:
if !a.stmt(stmt.Comm) {
return false
}
for _, s := range stmt.Body {
if !a.stmt(s) {
return false
}
}
return true
case *ast.DeclStmt:
return a.decl(stmt.Decl)
case *ast.DeferStmt:
return a.expr(stmt.Call)
case *ast.ExprStmt:
return !a.isObjOrNotExpr(stmt.X) // a.isObj(stmt.X) probably can't happen in a well-formed program.
case *ast.ForStmt:
if !a.stmt(stmt.Init) {
return false
}
if a.isObjOrNotExpr(stmt.Cond) {
return false
}
if !a.stmt(stmt.Post) {
return false
}
return a.stmt(stmt.Body)
case *ast.GoStmt:
return a.expr(stmt.Call)
case *ast.IfStmt:
if !a.stmt(stmt.Init) {
return false
}
if a.isObjOrNotExpr(stmt.Cond) {
return false
}
if !a.stmt(stmt.Body) {
return false
}
return a.stmt(stmt.Else)
case *ast.IncDecStmt:
return !a.isObjOrNotExpr(stmt.X)
case *ast.LabeledStmt:
return a.stmt(stmt.Stmt)
case *ast.RangeStmt:
// As with AssignStmt,
// if our object appears on the lhs we don't care.
if a.isObjOrNotExpr(stmt.X) {
return false
}
return a.stmt(stmt.Body)
case *ast.ReturnStmt:
for i, expr := range stmt.Results {
if a.isObj(expr) {
typ, fpos, ok := a.enclosingFuncInfo()
if !ok {
panic(errf("no type info for function containing return statement at %s", a.pos(expr)))
}
sig, ok := typ.(*types.Signature)
if !ok {
panic(errf("got %T, want *types.Signature for type of function at %s", typ, fpos))
}
if i >= sig.Results().Len() {
panic(errf("cannot return %d value(s) from %d-value-returning function at %s", i+1, sig.Results().Len(), a.pos(stmt)))
}
resultvar := sig.Results().At(i)
intf := getType[*types.Interface](resultvar.Type())
if intf == nil {
return false
}
a.addMethods(intf)
continue
}
if !a.expr(expr) {
return false
}
}
return true
case *ast.SelectStmt:
return a.stmt(stmt.Body)
case *ast.SendStmt:
if a.isObjOrNotExpr(stmt.Chan) {
return false
}
if a.isObj(stmt.Value) {
tv, ok := a.pkg.TypesInfo.Types[stmt.Chan]
if !ok {
panic(errf("no type info for channel in send statement at %s", a.pos(stmt)))
}
chtyp := getType[*types.Chan](tv.Type)
if chtyp == nil {
panic(errf("got %T, want channel for type of channel in send statement at %s", tv.Type, a.pos(stmt)))
}
intf := getType[*types.Interface](chtyp.Elem())
if intf == nil {
return false
}
a.addMethods(intf)
return true
}
return a.expr(stmt.Value)
case *ast.SwitchStmt:
return a.switchStmt(stmt)
case *ast.TypeSwitchStmt:
if !a.stmt(stmt.Init) {
return false
}
// Can skip stmt.Assign.
return a.stmt(stmt.Body)
}
return false
}
func (a *analyzer) pos(p interface{ Pos() token.Pos }) token.Position {
return a.pkg.Fset.Position(p.Pos())
}
type methoder interface {
NumMethods() int
Method(int) *types.Func
}
func (a *analyzer) addMethods(intf methoder) {
addMethodsToMap(intf, a.methods)
}
func addMethodsToMap(intf methoder, mm MethodMap) {
for i := 0; i < intf.NumMethods(); i++ {
m := intf.Method(i)
// m is a *types.Func, and the Type() of a *types.Func is always *types.Signature.
mm[m.Name()] = m.Type().(*types.Signature)
}
}
func (a *analyzer) expr(expr ast.Expr) (ok bool) {
a.level++
a.debugf("> expr %#v", expr)
defer func() {
a.debugf("< expr %#v %v", expr, ok)
a.level--
}()
if expr == nil {
return true
}
switch expr := expr.(type) {
case *ast.BinaryExpr:
var other ast.Expr
if a.isObj(expr.X) {
other = expr.Y
} else if a.isObj(expr.Y) {
other = expr.X
}
if other != nil {
switch expr.Op {
case token.EQL, token.NEQ:
if a.isObj(other) {
return true
}
tv, ok := a.pkg.TypesInfo.Types[other]
if !ok {
panic(errf("no type info for expr at %s", a.pos(other)))
}
intf := getType[*types.Interface](tv.Type)
if intf == nil {
return false
}
a.addMethods(intf)
// Continue below.
default:
return false
}
}
return a.expr(expr.X) && a.expr(expr.Y)
case *ast.CallExpr:
if a.isObjOrNotExpr(expr.Fun) {
return false
}
for i, arg := range expr.Args {
if a.isObj(arg) {
if i == len(expr.Args)-1 && expr.Ellipsis != token.NoPos {
// This is "obj..." using our object, requiring it to be a slice.
return false
}
tv, ok := a.pkg.TypesInfo.Types[expr.Fun]
if !ok {
panic(errf("no type info for function in call expression at %s", a.pos(expr)))
}
sig := getType[*types.Signature](tv.Type)
if sig == nil {
// This could be a type conversion expression; e.g. int(x).
if len(expr.Args) == 1 {
return false
}
panic(errf("got %T, want *types.Signature for type of function in call expression at %s", tv.Type, a.pos(expr)))
}
var (
params = sig.Params()
plen = params.Len()
ptype types.Type
)
if sig.Variadic() && i >= plen-1 {
ptype = params.At(plen - 1).Type()
slice, ok := ptype.(*types.Slice)
if !ok {
panic(errf("got %T, want slice for type of final parameter of variadic function in call expression at %s", ptype, a.pos(expr)))
}
ptype = slice.Elem()
} else if i >= plen {
panic(errf("cannot send %d argument(s) to %d-parameter function in call expression at %s", i+1, plen, a.pos(expr)))
} else {
ptype = params.At(i).Type()
}
intf := getType[*types.Interface](ptype)
if intf == nil {
return false
}
a.addMethods(intf)
continue
}
if !a.expr(arg) {
return false
}
}
return true
case *ast.CompositeLit:
// Can skip expr.Type.
for i, elt := range expr.Elts {
if kv, ok := elt.(*ast.KeyValueExpr); ok {
if a.isObj(kv.Key) {
tv, ok := a.pkg.TypesInfo.Types[expr]
if !ok {
panic(errf("no type info for composite literal at %s", a.pos(expr)))
}
mapType := getType[*types.Map](tv.Type)
if mapType == nil {
return false
}
intf := getType[*types.Interface](mapType.Key())
if intf == nil {
return false
}
a.addMethods(intf)
} else if !a.expr(kv.Key) {
return false
}
if a.isObj(kv.Value) {
tv, ok := a.pkg.TypesInfo.Types[expr]
if !ok {
panic(errf("no type info for composite literal at %s", a.pos(expr)))
}
literalType := tv.Type
if named, ok := literalType.(*types.Named); ok { // xxx should this be a loop?
literalType = named.Underlying()
}
var elemType types.Type
switch literalType := literalType.(type) {
case *types.Map:
elemType = literalType.Elem()
case *types.Struct:
id := getIdent(kv.Key)
if id == nil {
panic(errf("got %T, want *ast.Ident in key-value entry of struct-typed composite literal at %s", kv.Key, a.pos(kv)))
}
for j := 0; j < literalType.NumFields(); j++ {
field := literalType.Field(j)
if field.Name() == id.Name {
elemType = field.Type()
break
}
}
if elemType == nil {
panic(errf("assignment to unknown struct field %s at %s", id.Name, a.pos(kv)))
}
case *types.Slice:
elemType = literalType.Elem()
case *types.Array:
elemType = literalType.Elem()
default:
return false
}
intf := getType[*types.Interface](elemType)
if intf == nil {
return false
}
a.addMethods(intf)
} else if !a.expr(kv.Value) {
return false
}
continue
}
if a.isObj(elt) {
tv, ok := a.pkg.TypesInfo.Types[expr]
if !ok {
panic(errf("no type info for composite literal at %s", a.pos(expr)))
}
literalType := tv.Type
if named, ok := literalType.(*types.Named); ok { // xxx should this be a loop?
literalType = named.Underlying()
}
var elemType types.Type
switch literalType := literalType.(type) {
case *types.Struct:
if i >= literalType.NumFields() {
panic(errf("cannot assign field %d of %d-field struct at %s", i, literalType.NumFields(), a.pos(elt)))
}
elemType = literalType.Field(i).Type()
case *types.Slice:
elemType = literalType.Elem()
case *types.Array:
elemType = literalType.Elem()
}
intf := getType[*types.Interface](elemType)
if intf == nil {
return false
}
a.addMethods(intf)
continue
}
if !a.expr(elt) {
return false
}
}
return true
case *ast.Ellipsis:
return !a.isObjOrNotExpr(expr.Elt)
case *ast.FuncLit:
return a.funcLit(expr)
case *ast.Ident:
return true
case *ast.IndexExpr:
if a.isObjOrNotExpr(expr.X) {
return false
}
if a.isObj(expr.Index) {
// In expression x[index],
// index can be an interface
// if x is a map.
tv, ok := a.pkg.TypesInfo.Types[expr.X]
if !ok {
panic(errf("no type info for index expression at %s", a.pos(expr)))
}
mapType := getType[*types.Map](tv.Type)
if mapType == nil {
return false
}
intf := getType[*types.Interface](mapType.Key())
if intf == nil {
return false
}
a.addMethods(intf)
return true
}
return a.expr(expr.Index)
case *ast.IndexListExpr:
if a.isObjOrNotExpr(expr.X) {
return false
}
for _, idx := range expr.Indices {
if a.isObjOrNotExpr(idx) {
return false
}
}
return true
case *ast.KeyValueExpr:
panic("did not expect to reach the KeyValueExpr clause")
case *ast.ParenExpr:
return a.expr(expr.X)
case *ast.SelectorExpr:
if a.isObj(expr.X) {
if sig := a.getSig(expr); sig != nil {
a.methods[expr.Sel.Name] = sig
return true
}
return false
}
return a.expr(expr.X)
case *ast.SliceExpr:
if a.isObjOrNotExpr(expr.X) {
return false
}
if a.isObjOrNotExpr(expr.Low) {
return false
}
if a.isObjOrNotExpr(expr.High) {
return false
}
return !a.isObjOrNotExpr(expr.Max)
case *ast.StarExpr:
return !a.isObjOrNotExpr(expr.X)
case *ast.TypeAssertExpr:
// Can skip expr.Type.
return a.expr(expr.X)
case *ast.UnaryExpr:
if a.isObj(expr.X) {
return expr.Op == token.AND
}
return a.expr(expr.X)
}
return true
}
func (a *analyzer) isObjOrNotExpr(expr ast.Expr) bool {
if a.isObj(expr) {
return true
}
return !a.expr(expr)
}
func (a *analyzer) decl(decl ast.Decl) bool {
switch decl := decl.(type) {
case *ast.GenDecl:
if decl.Tok != token.VAR {
return true
}
for _, spec := range decl.Specs {
valspec, ok := spec.(*ast.ValueSpec)
if !ok {
panic(errf("got %T, want *ast.ValueSpec in variable declaration at %s", spec, a.pos(decl)))
}
for _, val := range valspec.Values {
if a.isObj(val) {
if valspec.Type == nil {
continue
}
tv, ok := a.pkg.TypesInfo.Types[valspec.Type]
if !ok {
panic(errf("no type info for variable declaration at %s", a.pos(valspec)))
}
intf := getType[*types.Interface](tv.Type)
if intf == nil {
return false
}
a.addMethods(intf)
continue
}
if !a.expr(val) {
return false
}
}
}
return true
case *ast.FuncDecl:
outer := a.enclosingFunc
a.enclosingFunc = &funcDeclOrLit{decl: decl}
defer func() { a.enclosingFunc = outer }()
return a.stmt(decl.Body)
default:
return true
}
}
func (a *analyzer) funcLit(expr *ast.FuncLit) bool {
outer := a.enclosingFunc
a.enclosingFunc = &funcDeclOrLit{lit: expr}
defer func() {
a.enclosingFunc = outer
}()
return a.stmt(expr.Body)
}
func (a *analyzer) switchStmt(stmt *ast.SwitchStmt) bool {
outer := a.enclosingSwitchStmt
a.enclosingSwitchStmt = stmt
defer func() {
a.enclosingSwitchStmt = outer
}()
if !a.stmt(stmt.Init) {
return false
}
// It's OK if stmt.Tag is our object.
if !a.expr(stmt.Tag) {
return false
}
return a.stmt(stmt.Body)
}
func getIdent(expr ast.Expr) *ast.Ident {
switch expr := expr.(type) {
case *ast.Ident:
return expr
case *ast.ParenExpr:
return getIdent(expr.X)
default:
return nil
}
}
func isInternal(path string) bool {
parts := strings.Split(path, "/")
return slices.Contains(parts, "internal")
}
func sameMethodMaps(a, b MethodMap) bool {
if len(a) != len(b) {
return false
}
for name, asig := range a {
bsig, ok := b[name]
if !ok {
return false
}
if !types.Identical(asig, bsig) {
return false
}
}
return true
}