| 1 | // Copyright 2018 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/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 10 | "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 11 | _ "unsafescip-go gomod github.com/golang/go/src go1.25.0 unsafe/" // for go:linkname hack |
| 12 | ) |
| 13 | |
| 14 | // CallKind describes the function position of an [*ast.CallExpr]. |
| 15 | type CallKindscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/CallKind# int |
| 16 | |
| 17 | const ( |
| 18 | CallStaticscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/CallStatic. CallKindscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/CallKind# = iota // static call to known function |
| 19 | CallInterfacescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/CallInterface. // dynamic call through an interface method |
| 20 | CallDynamicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/CallDynamic. // dynamic call of a func value |
| 21 | CallBuiltinscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/CallBuiltin. // call to a builtin function |
| 22 | CallConversionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/CallConversion. // a conversion (not a call) |
| 23 | ) |
| 24 | |
| 25 | var callKindNamesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/callKindNames. = []string{ |
| 26 | "CallStatic", |
| 27 | "CallInterface", |
| 28 | "CallDynamic", |
| 29 | "CallBuiltin", |
| 30 | "CallConversion", |
| 31 | } |
| 32 | |
| 33 | func (k CallKindscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/CallKind#) Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/CallKind#String().() string { |
| 34 | if i := int(k); i >= 0 && i < len(callKindNamesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/callKindNames.) { |
| 35 | return callKindNamesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/callKindNames.[i] |
| 36 | } |
| 37 | return 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().("typeutil.CallKind(%d)", k) |
| 38 | } |
| 39 | |
| 40 | // ClassifyCall classifies the function position of a call expression ([*ast.CallExpr]). |
| 41 | // It distinguishes among true function calls, calls to builtins, and type conversions, |
| 42 | // and further classifies function calls as static calls (where the function is known), |
| 43 | // dynamic interface calls, and other dynamic calls. |
| 44 | // |
| 45 | // For the declarations: |
| 46 | // |
| 47 | // func f() {} |
| 48 | // func g[T any]() {} |
| 49 | // var v func() |
| 50 | // var s []func() |
| 51 | // type I interface { M() } |
| 52 | // var i I |
| 53 | // |
| 54 | // ClassifyCall returns the following: |
| 55 | // |
| 56 | // f() CallStatic |
| 57 | // g[int]() CallStatic |
| 58 | // i.M() CallInterface |
| 59 | // min(1, 2) CallBuiltin |
| 60 | // v() CallDynamic |
| 61 | // s[0]() CallDynamic |
| 62 | // int(x) CallConversion |
| 63 | // []byte("") CallConversion |
| 64 | func ClassifyCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/ClassifyCall().(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#, call *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#) CallKindscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/CallKind# { |
| 65 | if info.Typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Types. == nil { |
| 66 | panic("ClassifyCall: info.Types is nil") |
| 67 | } |
| 68 | tv := info.Typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Types.[call.Funscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Fun.] |
| 69 | if tv.IsTypescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeAndValue#IsType().() { |
| 70 | return CallConversionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/CallConversion. |
| 71 | } |
| 72 | if tv.IsBuiltinscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeAndValue#IsBuiltin().() { |
| 73 | return CallBuiltinscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/CallBuiltin. |
| 74 | } |
| 75 | obj := info.Usesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Uses.[UsedIdentscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/UsedIdent().(info, call.Funscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Fun.)] |
| 76 | // Classify the call by the type of the object, if any. |
| 77 | switch obj := obj.(type) { |
| 78 | 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#: |
| 79 | if interfaceMethodscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/interfaceMethod().(obj) { |
| 80 | return CallInterfacescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/CallInterface. |
| 81 | } |
| 82 | return CallStaticscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/CallStatic. |
| 83 | default: |
| 84 | return CallDynamicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/CallDynamic. |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // UsedIdent returns the identifier such that info.Uses[UsedIdent(info, e)] |
| 89 | // is the [types.Object] used by e, if any. |
| 90 | // |
| 91 | // If e is one of various forms of reference: |
| 92 | // |
| 93 | // f, c, v, T lexical reference |
| 94 | // pkg.X qualified identifier |
| 95 | // f[T] or pkg.F[K,V] instantiations of the above kinds |
| 96 | // expr.f field or method value selector |
| 97 | // T.f method expression selector |
| 98 | // |
| 99 | // UsedIdent returns the identifier whose is associated value in [types.Info.Uses] |
| 100 | // is the object to which it refers. |
| 101 | // |
| 102 | // For the declarations: |
| 103 | // |
| 104 | // func F[T any] {...} |
| 105 | // type I interface { M() } |
| 106 | // var ( |
| 107 | // x int |
| 108 | // s struct { f int } |
| 109 | // a []int |
| 110 | // i I |
| 111 | // ) |
| 112 | // |
| 113 | // UsedIdent returns the following: |
| 114 | // |
| 115 | // Expr UsedIdent |
| 116 | // x x |
| 117 | // s.f f |
| 118 | // F[int] F |
| 119 | // i.M M |
| 120 | // I.M M |
| 121 | // min min |
| 122 | // int int |
| 123 | // 1 nil |
| 124 | // a[0] nil |
| 125 | // []byte nil |
| 126 | // |
| 127 | // Note: if e is an instantiated function or method, UsedIdent returns |
| 128 | // the corresponding generic function or method on the generic type. |
| 129 | func UsedIdentscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/UsedIdent().(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#, e astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Exprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Expr#) *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# { |
| 130 | return usedIdentscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/usedIdent().(info, e) |
| 131 | } |
| 132 | |
| 133 | //go:linkname usedIdent golang.org/x/tools/go/types/typeutil.usedIdent |
| 134 | func usedIdentscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/usedIdent().(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#, e astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Exprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Expr#) *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# |
| 135 | |
| 136 | //go:linkname interfaceMethod golang.org/x/tools/go/types/typeutil.interfaceMethod |
| 137 | func interfaceMethodscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/interfaceMethod().(f *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#) bool |