| 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 | // 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.Params(), true) // skip the Tuple |
| 65 | visit(sig.Results(), true) // skip the Tuple |
| 66 | } |
| 67 | |
| 68 | switch T := T.(type) { |
| 69 | case *types.Alias: |
| 70 | visit(types.Unalias(T), skip) // emulates the pre-Alias behavior |
| 71 | |
| 72 | case *types.Basic: |
| 73 | // nop |
| 74 | |
| 75 | case *types.Interface: |
| 76 | // nop---handled by recursion over method set. |
| 77 | |
| 78 | case *types.Pointer: |
| 79 | visit(T.Elem(), false) |
| 80 | |
| 81 | case *types.Slice: |
| 82 | visit(T.Elem(), false) |
| 83 | |
| 84 | case *types.Chan: |
| 85 | visit(T.Elem(), false) |
| 86 | |
| 87 | case *types.Map: |
| 88 | visit(T.Key(), false) |
| 89 | visit(T.Elem(), false) |
| 90 | |
| 91 | case *types.Signature: |
| 92 | if T.Recv() != nil { |
| 93 | panic(fmt.Sprintf("Signature %s has Recv %s", T, T.Recv())) |
| 94 | } |
| 95 | visit(T.Params(), true) // skip the Tuple |
| 96 | visit(T.Results(), true) // skip the Tuple |
| 97 | |
| 98 | case *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(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.Underlying(), true) // skip the unnamed type |
| 108 | |
| 109 | case *types.Array: |
| 110 | visit(T.Elem(), false) |
| 111 | |
| 112 | case *types.Struct: |
| 113 | for i, n := 0, T.NumFields(); i < n; i++ { |
| 114 | // TODO(adonovan): document whether or not |
| 115 | // it is safe to skip non-exported fields. |
| 116 | visit(T.Field(i).Type(), false) |
| 117 | } |
| 118 | |
| 119 | case *types.Tuple: |
| 120 | for i, n := 0, T.Len(); i < n; i++ { |
| 121 | visit(T.At(i).Type(), false) |
| 122 | } |
| 123 | |
| 124 | case *types.TypeParam, *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 | } |