| 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 typesinternal |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"go/types" |
| 10 |
|
| 11 |
"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 ForEachElement(rtypes *typeutil.Map, msets *typeutil.MethodSetCache, T types.Type, f func(types.Type)) { |
| 26 |
var visit func(T types.Type, skip bool) |
| 27 |
visit = func(T types.Type, skip bool) { |
| 28 |
if !skip { |
| 29 |
if seen, _ := rtypes.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.MethodSet(T) |
| 38 |
for method := range tmset.Methods() { |
| 39 |
sig := method.Type().(*types.Signature) |
| 40 |
if sig.TypeParams() != nil { |
| 41 |
continue // skip type-parameterized methods |
| 42 |
} |
| 43 |
|
| 44 |
// It is tempting to call visit(sig, false) |
| 45 |
// but, as noted in golang.org/cl/65450043, |
| 46 |
// the Signature.Recv field is ignored by |
| 47 |
// types.Identical and typeutil.Map, which |
| 48 |
// is confusing at best. |
| 49 |
// |
| 50 |
// More importantly, the true signature rtype |
| 51 |
// reachable from a method using reflection |
| 52 |
// has no receiver but an extra ordinary parameter. |
| 53 |
// For the Read method of io.Reader we want: |
| 54 |
// func(Reader, []byte) (int, error) |
| 55 |
// but here sig is: |
| 56 |
// func([]byte) (int, error) |
| 57 |
// with .Recv = Reader (though it is hard to |
| 58 |
// notice because it doesn't affect Signature.String |
| 59 |
// or types.Identical). |
| 60 |
// |
| 61 |
// TODO(adonovan): construct and visit the correct |
| 62 |
// non-method signature with an extra parameter |
| 63 |
// (though since unnamed func types have no methods |
| 64 |
// there is essentially no actual demand for this). |
| 65 |
// |
| 66 |
// TODO(adonovan): document whether or not it is |
| 67 |
// safe to skip non-exported methods (as RTA does). |
| 68 |
visit(sig.Params(), true) // skip the Tuple |
| 69 |
visit(sig.Results(), true) // skip the Tuple |
| 70 |
} |
| 71 |
|
| 72 |
switch T := T.(type) { |
| 73 |
case *types.Alias: |
| 74 |
visit(types.Unalias(T), skip) // emulates the pre-Alias behavior |
| 75 |
|
| 76 |
case *types.Basic: |
| 77 |
// nop |
| 78 |
|
| 79 |
case *types.Interface: |
| 80 |
// nop---handled by recursion over method set. |
| 81 |
|
| 82 |
case *types.Pointer: |
| 83 |
visit(T.Elem(), false) |
| 84 |
|
| 85 |
case *types.Slice: |
| 86 |
visit(T.Elem(), false) |
| 87 |
|
| 88 |
case *types.Chan: |
| 89 |
visit(T.Elem(), false) |
| 90 |
|
| 91 |
case *types.Map: |
| 92 |
visit(T.Key(), false) |
| 93 |
visit(T.Elem(), false) |
| 94 |
|
| 95 |
case *types.Signature: |
| 96 |
if T.Recv() != nil { |
| 97 |
panic(fmt.Sprintf("Signature %s has Recv %s", T, T.Recv())) |
| 98 |
} |
| 99 |
visit(T.Params(), true) // skip the Tuple |
| 100 |
visit(T.Results(), true) // skip the Tuple |
| 101 |
|
| 102 |
case *types.Named: |
| 103 |
// A pointer-to-named type can be derived from a named |
| 104 |
// type via reflection. It may have methods too. |
| 105 |
visit(types.NewPointer(T), false) |
| 106 |
|
| 107 |
// Consider 'type T struct{S}' where S has methods. |
| 108 |
// Reflection provides no way to get from T to struct{S}, |
| 109 |
// only to S, so the method set of struct{S} is unwanted, |
| 110 |
// so set 'skip' flag during recursion. |
| 111 |
visit(T.Underlying(), true) // skip the unnamed type |
| 112 |
|
| 113 |
case *types.Array: |
| 114 |
visit(T.Elem(), false) |
| 115 |
|
| 116 |
case *types.Struct: |
| 117 |
for i, n := 0, T.NumFields(); i < n; i++ { |
| 118 |
// TODO(adonovan): document whether or not |
| 119 |
// it is safe to skip non-exported fields. |
| 120 |
visit(T.Field(i).Type(), false) |
| 121 |
} |
| 122 |
|
| 123 |
case *types.Tuple: |
| 124 |
for i, n := 0, T.Len(); i < n; i++ { |
| 125 |
visit(T.At(i).Type(), false) |
| 126 |
} |
| 127 |
|
| 128 |
case *types.TypeParam, *types.Union: |
| 129 |
// forEachReachable must not be called on parameterized types. |
| 130 |
panic(fmt.Sprintf("ForEachElement called on type containing %T", T)) |
| 131 |
|
| 132 |
default: |
| 133 |
panic(fmt.Sprintf("ForEachElement called on unexpected type %T", T)) |
| 134 |
} |
| 135 |
} |
| 136 |
visit(T, false) |
| 137 |
} |