| 1 | // Copyright 2025 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 typeindex provides an [Index] of type information for a |
| 6 | // package, allowing efficient lookup of, say, whether a given symbol |
| 7 | // is referenced and, if so, where from; or of the [inspector.Cursor] for |
| 8 | // the declaration of a particular [types.Object] symbol. |
| 9 | package typeindexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/ |
| 10 | |
| 11 | import ( |
| 12 | "encoding/binaryscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/" |
| 13 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 14 | "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 15 | "iterscip-go gomod github.com/golang/go/src go1.25.0 iter/" |
| 16 | |
| 17 | "golang.org/x/tools/go/ast/edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/" |
| 18 | "golang.org/x/tools/go/ast/inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/" |
| 19 | "golang.org/x/tools/go/types/typeutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/" |
| 20 | "golang.org/x/tools/internal/astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/" |
| 21 | "golang.org/x/tools/internal/typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/" |
| 22 | ) |
| 23 | |
| 24 | // New constructs an Index for the package of type-annotated syntax |
| 25 | // |
| 26 | // TODO(adonovan): accept a FileSet too? |
| 27 | // We regret not requiring one in inspector.New. |
| 28 | func Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/New().(inspect *inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#, pkg *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#, info *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Infoscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#) *Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index# { |
| 29 | ix := &Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#{ |
| 30 | inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#inspect.: inspect, |
| 31 | infoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#info.: info, |
| 32 | packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#packages.: make(map[string]*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#), |
| 33 | defscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#def.: make(map[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Objectscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#]inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#), |
| 34 | usesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#uses.: make(map[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Objectscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#]*usesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/uses#), |
| 35 | } |
| 36 | |
| 37 | addPackage := func(pkg2 *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#) { |
| 38 | if pkg2 != nil && pkg2 != pkg { |
| 39 | ix.packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#packages.[pkg2.Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Path().()] = pkg2 |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | for cur := range inspect.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#Root().().Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Preorder().((*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.ImportSpecscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#)(nil), (*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#)(nil)) { |
| 44 | switch n := cur.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(type) { |
| 45 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.ImportSpecscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#: |
| 46 | // Index direct imports, including blank ones. |
| 47 | if pkgname := info.PkgNameOfscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#PkgNameOf().(n); pkgname != nil { |
| 48 | addPackage(pkgname.Importedscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/PkgName#Imported().()) |
| 49 | } |
| 50 | |
| 51 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#: |
| 52 | // Index all defining and using identifiers. |
| 53 | if obj := info.Defsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Defs.[n]; obj != nil { |
| 54 | ix.defscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#def.[obj] = cur |
| 55 | } |
| 56 | |
| 57 | if obj := info.Usesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Uses.[n]; obj != nil { |
| 58 | // Index indirect dependencies (via fields and methods). |
| 59 | if !typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.IsPackageLevelscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/IsPackageLevel().(obj) { |
| 60 | addPackage(obj.Pkgscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Pkg().()) |
| 61 | } |
| 62 | |
| 63 | for { |
| 64 | us, ok := ix.usesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#uses.[obj] |
| 65 | if !ok { |
| 66 | us = &usesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/uses#{} |
| 67 | us.codescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/uses#code. = us.initialscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/uses#initial.[:0] |
| 68 | ix.usesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#uses.[obj] = us |
| 69 | } |
| 70 | delta := cur.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Index().() - us.lastscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/uses#last. |
| 71 | if delta < 0 { |
| 72 | panic("non-monotonic") |
| 73 | } |
| 74 | us.codescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/uses#code. = binaryscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/.AppendUvarintscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/AppendUvarint().(us.codescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/uses#code., uint64(delta)) |
| 75 | us.lastscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/uses#last. = cur.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Index().() |
| 76 | |
| 77 | // If n is a selection of a field or method of an instantiated |
| 78 | // type, also record a use of the generic field or method. |
| 79 | obj, ok = objectOriginscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/objectOrigin().(obj) |
| 80 | if !ok { |
| 81 | break |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | return ix |
| 88 | } |
| 89 | |
| 90 | // objectOrigin returns the generic object for obj if it is a field or |
| 91 | // method of an instantied type; zero otherwise. |
| 92 | // |
| 93 | // (This operation is appropriate only for selections. |
| 94 | // Lexically resolved references always resolve to the generic. |
| 95 | // Although Named and Alias types also use Origin to express |
| 96 | // an instance/generic distinction, that's in the domain |
| 97 | // of Types; their TypeName objects always refer to the generic.) |
| 98 | func objectOriginscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/objectOrigin().(obj typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Objectscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#) (typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Objectscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#, bool) { |
| 99 | var origin typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Objectscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object# |
| 100 | switch obj := obj.(type) { |
| 101 | case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Funcscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Func#: |
| 102 | if obj.Signaturescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Func#Signature().().Recvscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#Recv().() != nil { |
| 103 | origin = obj.Originscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Func#Origin().() // G[int].method -> G[T].method |
| 104 | } |
| 105 | case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Varscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Var#: |
| 106 | if obj.IsFieldscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Var#IsField().() { |
| 107 | origin = obj.Originscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Var#Origin().() // G[int].field -> G[T].field |
| 108 | } |
| 109 | } |
| 110 | if origin != nil && origin != obj { |
| 111 | return origin, true |
| 112 | } |
| 113 | return nil, false |
| 114 | } |
| 115 | |
| 116 | // An Index holds an index mapping [types.Object] symbols to their syntax. |
| 117 | // In effect, it is the inverse of [types.Info]. |
| 118 | type Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index# struct { |
| 119 | inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#inspect. *inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector# |
| 120 | infoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#info. *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Infoscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info# |
| 121 | packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#packages. map[string]*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package# // packages of all symbols referenced from this package |
| 122 | defscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#def. map[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Objectscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#]inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor# // Cursor of *ast.Ident that defines the Object |
| 123 | usesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#uses. map[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Objectscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#]*usesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/uses# // Cursors of *ast.Idents that use the Object |
| 124 | } |
| 125 | |
| 126 | // A uses holds the list of Cursors of Idents that use a given symbol. |
| 127 | // |
| 128 | // The Uses map of [types.Info] is substantial, so it pays to compress |
| 129 | // its inverse mapping here, both in space and in CPU due to reduced |
| 130 | // allocation. A Cursor is 2 words; a Cursor.Index is 4 bytes; but |
| 131 | // since Cursors are naturally delivered in ascending order, we can |
| 132 | // use varint-encoded deltas at a cost of only ~1.7-2.2 bytes per use. |
| 133 | // |
| 134 | // Many variables have only one or two uses, so their encoded uses may |
| 135 | // fit in the 4 bytes of initial, saving further CPU and space |
| 136 | // essentially for free since the struct's size class is 4 words. |
| 137 | type usesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/uses# struct { |
| 138 | codescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/uses#code. []byte // varint-encoded deltas of successive Cursor.Index values |
| 139 | lastscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/uses#last. int32 // most recent Cursor.Index value; used during encoding |
| 140 | initialscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/uses#initial. [4]byte // use slack in size class as initial space for code |
| 141 | } |
| 142 | |
| 143 | // Uses returns the sequence of Cursors of [*ast.Ident]s in this package |
| 144 | // that refer to obj. If obj is nil, the sequence is empty. |
| 145 | // |
| 146 | // Uses, unlike the Uses field of [types.Info], records additional |
| 147 | // entries mapping fields and methods of generic types to references |
| 148 | // through their corresponding instantiated objects. |
| 149 | func (ix *Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#) Usesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Uses().(obj typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Objectscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#) iterscip-go gomod github.com/golang/go/src go1.25.0 iter/.Seqscip-go gomod github.com/golang/go/src go1.25.0 iter/Seq#[inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#] { |
| 150 | return func(yield func(inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#) bool) { |
| 151 | if uses := ix.usesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#uses.[obj]; uses != nil { |
| 152 | var last int32 |
| 153 | for code := uses.codescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/uses#code.; len(code) > 0; { |
| 154 | delta, n := binaryscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/.Uvarintscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/Uvarint().(code) |
| 155 | last += int32(delta) |
| 156 | if !yield(ix.inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#inspect..Atscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#At().(last)) { |
| 157 | return |
| 158 | } |
| 159 | code = code[n:] |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // Used reports whether any of the specified objects are used, in |
| 166 | // other words, obj != nil && Uses(obj) is non-empty for some obj in objs. |
| 167 | // |
| 168 | // (This treatment of nil allows Used to be called directly on the |
| 169 | // result of [Index.Object] so that analyzers can conveniently skip |
| 170 | // packages that don't use a symbol of interest.) |
| 171 | func (ix *Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#) Usedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Used().(objs ...typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Objectscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#) bool { |
| 172 | for _, obj := range objs { |
| 173 | if obj != nil && ix.usesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#uses.[obj] != nil { |
| 174 | return true |
| 175 | } |
| 176 | } |
| 177 | return false |
| 178 | } |
| 179 | |
| 180 | // Def returns the Cursor of the [*ast.Ident] in this package |
| 181 | // that declares the specified object, if any. |
| 182 | func (ix *Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#) Defscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Def().(obj typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Objectscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#) (inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#, bool) { |
| 183 | cur, ok := ix.defscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#def.[obj] |
| 184 | return cur, ok |
| 185 | } |
| 186 | |
| 187 | // Package returns the package of the specified path, |
| 188 | // or nil if it is not referenced from this package. |
| 189 | func (ix *Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#) Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Package().(path string) *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package# { |
| 190 | return ix.packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#packages.[path] |
| 191 | } |
| 192 | |
| 193 | // Object returns the package-level symbol name within the package of |
| 194 | // the specified path, or nil if the package or symbol does not exist |
| 195 | // or is not visible from this package. |
| 196 | func (ix *Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#) Objectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Object().(path, name string) typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Objectscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object# { |
| 197 | if pkg := ix.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Package().(path); pkg != nil { |
| 198 | return pkg.Scopescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Scope().().Lookupscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Scope#Lookup().(name) |
| 199 | } |
| 200 | return nil |
| 201 | } |
| 202 | |
| 203 | // Selection returns the named method or field belonging to the |
| 204 | // package-level type returned by Object(path, typename). |
| 205 | func (ix *Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#) Selectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Selection().(path, typename, name string) typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Objectscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object# { |
| 206 | if obj := ix.Objectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Object().(path, typename); obj != nil { |
| 207 | if tname, ok := obj.(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.TypeNamescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeName#); ok { |
| 208 | obj, _, _ := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.LookupFieldOrMethodscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/LookupFieldOrMethod().(tname.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Type().(), true, obj.Pkgscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Pkg().(), name) |
| 209 | return obj |
| 210 | } |
| 211 | } |
| 212 | return nil |
| 213 | } |
| 214 | |
| 215 | // Calls returns the sequence of cursors for *ast.CallExpr nodes that |
| 216 | // call the specified callee, as defined by [typeutil.Callee]. |
| 217 | // If callee is nil, the sequence is empty. |
| 218 | func (ix *Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#) Callsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Calls().(callee typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Objectscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#) iterscip-go gomod github.com/golang/go/src go1.25.0 iter/.Seqscip-go gomod github.com/golang/go/src go1.25.0 iter/Seq#[inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#] { |
| 219 | return func(yield func(inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#) bool) { |
| 220 | for cur := range ix.Usesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Uses().(callee) { |
| 221 | // The call may be of the form f() or x.f(), |
| 222 | // optionally with parens; ascend from f to call. |
| 223 | // See logic in [typesinternal.UsedIdent], to which this is dual. |
| 224 | // |
| 225 | // It is tempting but wrong to use the first |
| 226 | // CallExpr ancestor: we have to make sure the |
| 227 | // ident is in the CallExpr.Fun position, otherwise |
| 228 | // f(f, f) would have two spurious matches. |
| 229 | // Avoiding Enclosing is also significantly faster. |
| 230 | |
| 231 | // inverse unparen: f -> (f) |
| 232 | cur = astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.UnparenEnclosingCursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/UnparenEnclosingCursor().(cur) |
| 233 | |
| 234 | // ascend selector (or qualified identifier): f -> x.f |
| 235 | if cur.ParentEdgeKindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#ParentEdgeKind().() == edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/.SelectorExpr_Selscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/SelectorExpr_Sel. { |
| 236 | cur = astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.UnparenEnclosingCursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/UnparenEnclosingCursor().(cur.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().()) |
| 237 | } |
| 238 | |
| 239 | // ascend typeparams: f -> f[T]; f -> f[T1, T2] |
| 240 | if ek := cur.ParentEdgeKindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#ParentEdgeKind().(); ek == edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/.IndexExpr_Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/IndexExpr_X. || ek == edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/.IndexListExpr_Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/IndexListExpr_X. { |
| 241 | cur = astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.UnparenEnclosingCursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/UnparenEnclosingCursor().(cur.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().()) |
| 242 | } |
| 243 | |
| 244 | // ascend from f or x.f to call |
| 245 | if cur.ParentEdgeKindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#ParentEdgeKind().() == edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/.CallExpr_Funscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/CallExpr_Fun. { |
| 246 | curCall := cur.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().() |
| 247 | call := curCall.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#) |
| 248 | if typeutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/.Calleescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/Callee().(ix.infoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#info., call) == callee { |
| 249 | if !yield(curCall) { |
| 250 | return |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | } |