Debian Codegraph @

typeparams/example/findtypeparams/main.go

raw symbols SCIP
1 // Copyright 2021 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 hello = `
20 //!+input
21 package main
22
23 type Constraint interface {
24 Value() any
25 }
26
27 type Pair[L, R any] struct {
28 left L
29 right R
30 }
31
32 func MakePair[L, R Constraint](l L, r R) Pair[L, R] {
33 return Pair[L, R]{l, r}
34 }
35 //!-input
36 `
37
38 // !+print
39 func PrintTypeParams(fset *token.FileSet, file *ast.File) error {
40 conf := types.Config{Importer: importer.Default()}
41 info := &types.Info{
42 Scopes: make(map[ast.Node]*types.Scope),
43 Defs: make(map[*ast.Ident]types.Object),
44 }
45 _, err := conf.Check("hello", fset, []*ast.File{file}, info)
46 if err != nil {
47 return err
48 }
49
50 // For convenience, we can use ast.Inspect to find the nodes we want to
51 // investigate.
52 ast.Inspect(file, func(n ast.Node) bool {
53 var name *ast.Ident // the name of the generic object, or nil
54 var tparamFields *ast.FieldList // the list of type parameter fields
55 var tparams *types.TypeParamList // the list of type parameter types
56 var scopeNode ast.Node // the node associated with the declaration scope
57
58 switch n := n.(type) {
59 case *ast.TypeSpec:
60 name = n.Name
61 tparamFields = n.TypeParams
62 tparams = info.Defs[name].Type().(*types.Named).TypeParams()
63 scopeNode = n
64 case *ast.FuncDecl:
65 name = n.Name
66 tparamFields = n.Type.TypeParams
67 tparams = info.Defs[name].Type().(*types.Signature).TypeParams()
68 scopeNode = n.Type
69 default:
70 // Not a type or function declaration.
71 return true
72 }
73
74 // Option 1: find type parameters by looking at their declaring field list.
75 if tparamFields != nil {
76 fmt.Printf("%s has a type parameter field list with %d fields\n", name.Name, tparamFields.NumFields())
77 for _, field := range tparamFields.List {
78 for _, name := range field.Names {
79 tparam := info.Defs[name]
80 fmt.Printf(" field %s defines an object %q\n", name.Name, tparam)
81 }
82 }
83 } else {
84 fmt.Printf("%s does not have a type parameter list\n", name.Name)
85 }
86
87 // Option 2: find type parameters via the TypeParams() method on the
88 // generic type.
89 if tparams.Len() > 0 {
90 fmt.Printf("%s has %d type parameters:\n", name.Name, tparams.Len())
91 for i := 0; i < tparams.Len(); i++ {
92 tparam := tparams.At(i)
93 fmt.Printf(" %s has constraint %s\n", tparam, tparam.Constraint())
94 }
95 } else {
96 fmt.Printf("%s does not have type parameters\n", name.Name)
97 }
98
99 // Option 3: find type parameters by looking in the declaration scope.
100 scope, ok := info.Scopes[scopeNode]
101 if ok {
102 fmt.Printf("%s has a scope with %d objects:\n", name.Name, scope.Len())
103 for _, name := range scope.Names() {
104 fmt.Printf(" %s is a %T\n", name, scope.Lookup(name))
105 }
106 } else {
107 fmt.Printf("%s does not have a scope\n", name.Name)
108 }
109
110 return true
111 })
112 return nil
113 }
114
115 //!-print
116
117 /*
118 //!+output
119 > go run golang.org/x/tools/internal/typeparams/example/findtypeparams
120 Constraint does not have a type parameter list
121 Constraint does not have type parameters
122 Constraint does not have a scope
123 Pair has a type parameter field list with 2 fields
124 field L defines an object "type parameter L any"
125 field R defines an object "type parameter R any"
126 Pair has 2 type parameters:
127 L has constraint any
128 R has constraint any
129 Pair has a scope with 2 objects:
130 L is a *types.TypeName
131 R is a *types.TypeName
132 MakePair has a type parameter field list with 2 fields
133 field L defines an object "type parameter L hello.Constraint"
134 field R defines an object "type parameter R hello.Constraint"
135 MakePair has 2 type parameters:
136 L has constraint hello.Constraint
137 R has constraint hello.Constraint
138 MakePair has a scope with 4 objects:
139 L is a *types.TypeName
140 R is a *types.TypeName
141 l is a *types.Var
142 r is a *types.Var
143 //!-output
144 */
145
146 func main() {
147 // Parse one file.
148 fset := token.NewFileSet()
149 f, err := parser.ParseFile(fset, "hello.go", hello, 0)
150 if err != nil {
151 log.Fatal(err)
152 }
153 if err := PrintTypeParams(fset, f); err != nil {
154 log.Fatal(err)
155 }
156 }