Debian Codegraph @

internal/typesinternal/element.go

raw symbols SCIP
1 // Copyright 2024 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 package typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/
6
7 import (
8 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
9 "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/"
10
11 "golang.org/x/tools/go/types/typeutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/"
12 )
13
14 // ForEachElement calls f for type T and each type reachable from its
15 // type through reflection. It does this by recursively stripping off
16 // type constructors; in addition, for each named type N, the type *N
17 // is added to the result as it may have additional methods.
18 //
19 // The caller must provide an initially empty set used to de-duplicate
20 // identical types, potentially across multiple calls to ForEachElement.
21 // (Its final value holds all the elements seen, matching the arguments
22 // passed to f.)
23 //
24 // TODO(adonovan): share/harmonize with go/callgraph/rta.
25 func ForEachElementscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/ForEachElement().(rtypes *typeutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/.Mapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/Map#, msets *typeutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/.MethodSetCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#, T typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#, f func(typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#)) {
26 var visit func(T typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#, skip bool)
27 visit = func(T typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#, skip bool) {
28 if !skip {
29 if seen, _ := rtypes.Setscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/Map#Set().(T, true).(bool); seen {
30 return // de-dup
31 }
32
33 f(T) // notify caller of new element type
34 }
35
36 // Recursion over signatures of each method.
37 tmset := msets.MethodSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#MethodSet().(T)
38 for method := range tmset.Methodsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/MethodSet#Methods().() {
39 sig := method.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Selection#Type().().(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Signaturescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#)
40 // It is tempting to call visit(sig, false)
41 // but, as noted in golang.org/cl/65450043,
42 // the Signature.Recv field is ignored by
43 // types.Identical and typeutil.Map, which
44 // is confusing at best.
45 //
46 // More importantly, the true signature rtype
47 // reachable from a method using reflection
48 // has no receiver but an extra ordinary parameter.
49 // For the Read method of io.Reader we want:
50 // func(Reader, []byte) (int, error)
51 // but here sig is:
52 // func([]byte) (int, error)
53 // with .Recv = Reader (though it is hard to
54 // notice because it doesn't affect Signature.String
55 // or types.Identical).
56 //
57 // TODO(adonovan): construct and visit the correct
58 // non-method signature with an extra parameter
59 // (though since unnamed func types have no methods
60 // there is essentially no actual demand for this).
61 //
62 // TODO(adonovan): document whether or not it is
63 // safe to skip non-exported methods (as RTA does).
64 visit(sig.Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#Params().(), true) // skip the Tuple
65 visit(sig.Resultsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#Results().(), true) // skip the Tuple
66 }
67
68 switch T := T.(type) {
69 case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Aliasscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Alias#:
70 visit(typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Unaliasscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Unalias().(T), skip) // emulates the pre-Alias behavior
71
72 case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Basicscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Basic#:
73 // nop
74
75 case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Interfacescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Interface#:
76 // nop---handled by recursion over method set.
77
78 case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Pointerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Pointer#:
79 visit(T.Elemscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Pointer#Elem().(), false)
80
81 case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Slicescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Slice#:
82 visit(T.Elemscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Slice#Elem().(), false)
83
84 case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Chanscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Chan#:
85 visit(T.Elemscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Chan#Elem().(), false)
86
87 case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Mapscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Map#:
88 visit(T.Keyscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Map#Key().(), false)
89 visit(T.Elemscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Map#Elem().(), false)
90
91 case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Signaturescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#:
92 if T.Recvscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#Recv().() != nil {
93 panic(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("Signature %s has Recv %s", T, T.Recvscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#Recv().()))
94 }
95 visit(T.Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#Params().(), true) // skip the Tuple
96 visit(T.Resultsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#Results().(), true) // skip the Tuple
97
98 case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Namedscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#:
99 // A pointer-to-named type can be derived from a named
100 // type via reflection. It may have methods too.
101 visit(typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewPointerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewPointer().(T), false)
102
103 // Consider 'type T struct{S}' where S has methods.
104 // Reflection provides no way to get from T to struct{S},
105 // only to S, so the method set of struct{S} is unwanted,
106 // so set 'skip' flag during recursion.
107 visit(T.Underlyingscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#Underlying().(), true) // skip the unnamed type
108
109 case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Arrayscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Array#:
110 visit(T.Elemscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Array#Elem().(), false)
111
112 case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Structscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Struct#:
113 for i, n := 0, T.NumFieldsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Struct#NumFields().(); i < n; i++ {
114 // TODO(adonovan): document whether or not
115 // it is safe to skip non-exported fields.
116 visit(T.Fieldscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Struct#Field().(i).Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Type().(), false)
117 }
118
119 case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Tuplescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Tuple#:
120 for i, n := 0, T.Lenscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Tuple#Len().(); i < n; i++ {
121 visit(T.Atscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Tuple#At().(i).Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Type().(), false)
122 }
123
124 case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.TypeParamscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeParam#, *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Unionscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Union#:
125 // forEachReachable must not be called on parameterized types.
126 panic(T)
127
128 default:
129 panic(T)
130 }
131 }
132 visit(T, false)
133 }