Debian Codegraph @

internal/typesinternal/classify_call.go

raw symbols SCIP
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 typesinternal
6
7 import (
8 "fmt"
9 "go/ast"
10 "go/types"
11 _ "unsafe" // for go:linkname hack
12 )
13
14 // CallKind describes the function position of an [*ast.CallExpr].
15 type CallKind int
16
17 const (
18 CallStatic CallKind = iota // static call to known function
19 CallInterface // dynamic call through an interface method
20 CallDynamic // dynamic call of a func value
21 CallBuiltin // call to a builtin function
22 CallConversion // a conversion (not a call)
23 )
24
25 var callKindNames = []string{
26 "CallStatic",
27 "CallInterface",
28 "CallDynamic",
29 "CallBuiltin",
30 "CallConversion",
31 }
32
33 func (k CallKind) String() string {
34 if i := int(k); i >= 0 && i < len(callKindNames) {
35 return callKindNames[i]
36 }
37 return 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 ClassifyCall(info *types.Info, call *ast.CallExpr) CallKind {
65 if info.Types == nil {
66 panic("ClassifyCall: info.Types is nil")
67 }
68 tv := info.Types[call.Fun]
69 if tv.IsType() {
70 return CallConversion
71 }
72 if tv.IsBuiltin() {
73 return CallBuiltin
74 }
75 obj := info.Uses[UsedIdent(info, call.Fun)]
76 // Classify the call by the type of the object, if any.
77 switch obj := obj.(type) {
78 case *types.Func:
79 if interfaceMethod(obj) {
80 return CallInterface
81 }
82 return CallStatic
83 default:
84 return 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 UsedIdent(info *types.Info, e ast.Expr) *ast.Ident {
130 return usedIdent(info, e)
131 }
132
133 //go:linkname usedIdent golang.org/x/tools/go/types/typeutil.usedIdent
134 func usedIdent(info *types.Info, e ast.Expr) *ast.Ident
135
136 //go:linkname interfaceMethod golang.org/x/tools/go/types/typeutil.interfaceMethod
137 func interfaceMethod(f *types.Func) bool