| 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 chautil provides helper functions related to |
| 6 | // class hierarchy analysis (CHA) for use in x/tools. |
| 7 | package chautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/internal/chautil`/ |
| 8 | |
| 9 | import ( |
| 10 | "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 11 | |
| 12 | "golang.org/x/tools/go/ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/" |
| 13 | "golang.org/x/tools/go/types/typeutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/" |
| 14 | ) |
| 15 | |
| 16 | // LazyCallees returns a function that maps a call site (in a function in fns) |
| 17 | // to its callees within fns. The set of callees is computed using the CHA algorithm, |
| 18 | // i.e., on the entire implements relation between interfaces and concrete types |
| 19 | // in fns. Please see golang.org/x/tools/go/callgraph/cha for more information. |
| 20 | // |
| 21 | // The resulting function is not concurrency safe. |
| 22 | func LazyCalleesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/internal/chautil`/LazyCallees().(fns map[*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#]bool) func(site ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.CallInstructionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallInstruction#) []*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function# { |
| 23 | // funcsBySig contains all functions, keyed by signature. It is |
| 24 | // the effective set of address-taken functions used to resolve |
| 25 | // a dynamic call of a particular signature. |
| 26 | var funcsBySig 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# // value is []*ssa.Function |
| 27 | |
| 28 | // methodsByID contains all methods, grouped by ID for efficient |
| 29 | // lookup. |
| 30 | // |
| 31 | // We must key by ID, not name, for correct resolution of interface |
| 32 | // calls to a type with two (unexported) methods spelled the same but |
| 33 | // from different packages. The fact that the concrete type implements |
| 34 | // the interface does not mean the call dispatches to both methods. |
| 35 | methodsByID := make(map[string][]*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#) |
| 36 | |
| 37 | // An imethod represents an interface method I.m. |
| 38 | // (There's no go/types object for it; |
| 39 | // a *types.Func may be shared by many interfaces due to interface embedding.) |
| 40 | type imethod struct { |
| 41 | I *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# |
| 42 | id string |
| 43 | } |
| 44 | // methodsMemo records, for every abstract method call I.m on |
| 45 | // interface type I, the set of concrete methods C.m of all |
| 46 | // types C that satisfy interface I. |
| 47 | // |
| 48 | // Abstract methods may be shared by several interfaces, |
| 49 | // hence we must pass I explicitly, not guess from m. |
| 50 | // |
| 51 | // methodsMemo is just a cache, so it needn't be a typeutil.Map. |
| 52 | methodsMemo := make(map[imethod][]*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#) |
| 53 | lookupMethods := func(I *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#, m *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#) []*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function# { |
| 54 | id := m.Idscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Id().() |
| 55 | methods, ok := methodsMemo[imethod{I, id}] |
| 56 | if !ok { |
| 57 | for _, f := range methodsByID[id] { |
| 58 | C := f.Signaturescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Signature..Recvscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#Recv().().Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Type().() // named or *named |
| 59 | if typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Implementsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Implements().(C, I) { |
| 60 | methods = append(methods, f) |
| 61 | } |
| 62 | } |
| 63 | methodsMemo[imethod{I, id}] = methods |
| 64 | } |
| 65 | return methods |
| 66 | } |
| 67 | |
| 68 | for f := range fns { |
| 69 | if f.Signaturescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Signature..Recvscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#Recv().() == nil { |
| 70 | // Package initializers can never be address-taken. |
| 71 | if f.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Name().() == "init" && f.Syntheticscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Synthetic. == "package initializer" { |
| 72 | continue |
| 73 | } |
| 74 | funcs, _ := funcsBySig.Atscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/Map#At().(f.Signaturescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Signature.).([]*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#) |
| 75 | funcs = append(funcs, f) |
| 76 | funcsBySig.Setscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/Map#Set().(f.Signaturescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Signature., funcs) |
| 77 | } else if obj := f.Objectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Object().(); obj != nil { |
| 78 | id := obj.(*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#).Idscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Id().() |
| 79 | methodsByID[id] = append(methodsByID[id], f) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return func(site ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.CallInstructionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallInstruction#) []*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function# { |
| 84 | call := site.Commonscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallInstruction#Common.() |
| 85 | if call.IsInvokescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#IsInvoke().() { |
| 86 | tiface := call.Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#Value..Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Value#Type.().Underlyingscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#Underlying().().(*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#) |
| 87 | return lookupMethods(tiface, call.Methodscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#Method.) |
| 88 | } else if g := call.StaticCalleescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#StaticCallee().(); g != nil { |
| 89 | return []*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#{g} |
| 90 | } else if _, ok := call.Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#Value..(*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Builtinscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Builtin#); !ok { |
| 91 | fns, _ := funcsBySig.Atscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/Map#At().(call.Signaturescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#Signature().()).([]*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#) |
| 92 | return fns |
| 93 | } |
| 94 | return nil |
| 95 | } |
| 96 | } |