Debian Codegraph @

typeparams/example/methoddecls/main.go

raw symbols SCIP
1 // Copyright 2022 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build go1.18
6
7 package main
8
9 import (
10 "fmt"
11 "go/ast"
12 "go/importer"
13 "go/parser"
14 "go/token"
15 "go/types"
16 "log"
17 )
18
19 const methods = `
20 //!+input
21 package methods
22
23 type List[E any] []E
24
25 func (l List[_]) Len() int {
26 return len(l)
27 }
28
29 func (l *List[E]) Append(v E) {
30 *l = append(*l, v)
31 }
32
33 type Pair[L, R comparable] struct {
34 left L
35 right R
36 }
37
38 func (p Pair[L, _]) Left() L {
39 return p.left
40 }
41
42 func (p Pair[_, R]) Right() R {
43 return p.right
44 }
45
46 func (p Pair[L, R]) Equal(other Pair[L, R]) bool {
47 return p.Left() == other.Left() && p.Right() == other.Right()
48 }
49 //!-input
50 `
51
52 // !+describe
53 func Describe(fset *token.FileSet, file *ast.File) error {
54 conf := types.Config{Importer: importer.Default()}
55 info := &types.Info{
56 Defs: make(map[*ast.Ident]types.Object),
57 }
58 pkg, err := conf.Check("pair", fset, []*ast.File{file}, info)
59 if err != nil {
60 return err
61 }
62 for _, name := range pkg.Scope().Names() {
63 obj := pkg.Scope().Lookup(name)
64 typ := obj.Type().(*types.Named)
65
66 fmt.Printf("type %s has %d methods\n", name, typ.NumMethods())
67 for i := 0; i < typ.NumMethods(); i++ {
68 m := typ.Method(i)
69 sig := m.Type().(*types.Signature)
70 recv := sig.Recv().Type()
71 fmt.Printf(" %s has %d receiver type parameters\n", m.Name(), sig.RecvTypeParams().Len())
72 fmt.Printf(" ...and receiver type %+v\n", recv)
73 }
74 }
75 // }
76
77 // func foo() {}
78 for _, decl := range file.Decls {
79 fdecl, ok := decl.(*ast.FuncDecl)
80 if !ok {
81 continue
82 }
83 fmt.Printf("Declaration of %q has receiver node type %T\n", fdecl.Name, fdecl.Recv.List[0].Type)
84 declObj := info.Defs[fdecl.Name]
85 recvIdent := receiverTypeName(fdecl.Recv.List[0].Type)
86 typ := pkg.Scope().Lookup(recvIdent.Name).Type().(*types.Named)
87 // ptr := types.NewPointer(typ)
88 name := fdecl.Name.Name
89 methodObj, _, _ := types.LookupFieldOrMethod(typ, false, pkg, name)
90 if declObj == methodObj {
91 fmt.Printf(" info.Uses[%s] == types.LookupFieldOrMethod(%s, ..., %q)\n", fdecl.Name, typ, name)
92 }
93 }
94 return nil
95 }
96
97 func receiverTypeName(e ast.Expr) *ast.Ident {
98 if s, ok := e.(*ast.StarExpr); ok {
99 e = s.X
100 }
101 switch e := e.(type) {
102 case *ast.IndexExpr:
103 return e.X.(*ast.Ident)
104 case *ast.IndexListExpr:
105 return e.X.(*ast.Ident)
106 }
107 panic("unexpected receiver node type")
108 }
109
110 //!-describe
111
112 /*
113 //!+output
114 > go run golang.org/x/tools/internal/typeparams/example/methoddecls
115 Pair has 2 methods
116 Left has 2 receiver type parameters
117 ...and receiver type pair.Pair[L, _]
118 Right has 2 receiver type parameters
119 ...and receiver type pair.Pair[_, R]
120 Declaration of "Left" has receiver node type *ast.IndexListExpr
121 info.Uses[Left] == types.LookupFieldOrMethod(pair.Pair[L, R any], ..., "Left")
122 Declaration of "Right" has receiver node type *ast.IndexListExpr
123 info.Uses[Right] == types.LookupFieldOrMethod(pair.Pair[L, R any], ..., "Right")
124 //!-output
125 */
126
127 func main() {
128 // Parse one file.
129 fset := token.NewFileSet()
130 f, err := parser.ParseFile(fset, "methods.go", methods, 0)
131 if err != nil {
132 log.Fatal(err)
133 }
134 if err := Describe(fset, f); err != nil {
135 log.Fatal(err)
136 }
137 }