File: main.go

package info (click to toggle)
golang-golang-x-exp 0.0~git20230522.2e198f4-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 6,404 kB
  • sloc: ansic: 1,900; objc: 276; sh: 272; asm: 48; makefile: 26
file content (138 lines) | stat: -rw-r--r-- 3,234 bytes parent folder | download | duplicates (2)
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
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build go1.18
// +build go1.18

package main

import (
	"fmt"
	"go/ast"
	"go/importer"
	"go/parser"
	"go/token"
	"go/types"
	"log"
)

const methods = `
//!+input
package methods

type List[E any] []E

func (l List[_]) Len() int {
	return len(l)
}

func (l *List[E]) Append(v E) {
	*l = append(*l, v)
}

type Pair[L, R comparable] struct {
	left  L
	right R
}

func (p Pair[L, _]) Left() L {
	return p.left
}

func (p Pair[_, R]) Right() R {
	return p.right
}

func (p Pair[L, R]) Equal(other Pair[L, R]) bool {
	return p.Left() == other.Left() && p.Right() == other.Right()
}
//!-input
`

// !+describe
func Describe(fset *token.FileSet, file *ast.File) error {
	conf := types.Config{Importer: importer.Default()}
	info := &types.Info{
		Defs: make(map[*ast.Ident]types.Object),
	}
	pkg, err := conf.Check("pair", fset, []*ast.File{file}, info)
	if err != nil {
		return err
	}
	for _, name := range pkg.Scope().Names() {
		obj := pkg.Scope().Lookup(name)
		typ := obj.Type().(*types.Named)

		fmt.Printf("type %s has %d methods\n", name, typ.NumMethods())
		for i := 0; i < typ.NumMethods(); i++ {
			m := typ.Method(i)
			sig := m.Type().(*types.Signature)
			recv := sig.Recv().Type()
			fmt.Printf("  %s has %d receiver type parameters\n", m.Name(), sig.RecvTypeParams().Len())
			fmt.Printf("  ...and receiver type %+v\n", recv)
		}
	}
	// }

	// func foo() {}
	for _, decl := range file.Decls {
		fdecl, ok := decl.(*ast.FuncDecl)
		if !ok {
			continue
		}
		fmt.Printf("Declaration of %q has receiver node type %T\n", fdecl.Name, fdecl.Recv.List[0].Type)
		declObj := info.Defs[fdecl.Name]
		recvIdent := receiverTypeName(fdecl.Recv.List[0].Type)
		typ := pkg.Scope().Lookup(recvIdent.Name).Type().(*types.Named)
		// ptr := types.NewPointer(typ)
		name := fdecl.Name.Name
		methodObj, _, _ := types.LookupFieldOrMethod(typ, false, pkg, name)
		if declObj == methodObj {
			fmt.Printf("  info.Uses[%s] == types.LookupFieldOrMethod(%s, ..., %q)\n", fdecl.Name, typ, name)
		}
	}
	return nil
}

func receiverTypeName(e ast.Expr) *ast.Ident {
	if s, ok := e.(*ast.StarExpr); ok {
		e = s.X
	}
	switch e := e.(type) {
	case *ast.IndexExpr:
		return e.X.(*ast.Ident)
	case *ast.IndexListExpr:
		return e.X.(*ast.Ident)
	}
	panic("unexpected receiver node type")
}

//!-describe

/*
//!+output
> go run golang.org/x/tools/internal/typeparams/example/methoddecls
Pair has 2 methods
  Left has 2 receiver type parameters
  ...and receiver type pair.Pair[L, _]
  Right has 2 receiver type parameters
  ...and receiver type pair.Pair[_, R]
Declaration of "Left" has receiver node type *ast.IndexListExpr
  info.Uses[Left] == types.LookupFieldOrMethod(pair.Pair[L, R any], ..., "Left")
Declaration of "Right" has receiver node type *ast.IndexListExpr
  info.Uses[Right] == types.LookupFieldOrMethod(pair.Pair[L, R any], ..., "Right")
//!-output
*/

func main() {
	// Parse one file.
	fset := token.NewFileSet()
	f, err := parser.ParseFile(fset, "methods.go", methods, 0)
	if err != nil {
		log.Fatal(err)
	}
	if err := Describe(fset, f); err != nil {
		log.Fatal(err)
	}
}