Debian Codegraph @

internal/xtools-internal/typesinternal/classify_call.go

raw ◀ ▶
1 Debian patch touch this file
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 )
12
13 // CallKind describes the function position of an [*ast.CallExpr].
14 type CallKind int
15
16 const (
17 CallStatic CallKind = iota // static call to known function
18 CallInterface // dynamic call through an interface method
19 CallDynamic // dynamic call of a func value
20 CallBuiltin // call to a builtin function
21 CallConversion // a conversion (not a call)
22 )
23
24 var callKindNames = []string{
25 "CallStatic",
26 "CallInterface",
27 "CallDynamic",
28 "CallBuiltin",
29 "CallConversion",
30 }
31
32 func (k CallKind) String() string {
33 if i := int(k); i >= 0 && i < len(callKindNames) {
34 return callKindNames[i]
35 }
36 return fmt.Sprintf("typeutil.CallKind(%d)", k)
37 }
38
39 // ClassifyCall classifies the function position of a call expression ([*ast.CallExpr]).
40 // It distinguishes among true function calls, calls to builtins, and type conversions,
41 // and further classifies function calls as static calls (where the function is known),
42 // dynamic interface calls, and other dynamic calls.
43 //
44 // For the declarations:
45 //
46 // func f() {}
47 // func g[T any]() {}
48 // var v func()
49 // var s []func()
50 // type I interface { M() }
51 // var i I
52 //
53 // ClassifyCall returns the following:
54 //
55 // f() CallStatic
56 // g[int]() CallStatic
57 // i.M() CallInterface
58 // min(1, 2) CallBuiltin
59 // v() CallDynamic
60 // s[0]() CallDynamic
61 // int(x) CallConversion
62 // []byte("") CallConversion
63 func ClassifyCall(info *types.Info, call *ast.CallExpr) CallKind {
64 if info.Types == nil {
65 panic("ClassifyCall: info.Types is nil")
66 }
67 tv := info.Types[call.Fun]
68 if tv.IsType() {
69 return CallConversion
70 }
71 if tv.IsBuiltin() {
72 return CallBuiltin
73 }
74 obj := info.Uses[UsedIdent(info, call.Fun)]
75 // Classify the call by the type of the object, if any.
76 switch obj := obj.(type) {
77 case *types.Func:
78 if interfaceMethod(obj) {
79 return CallInterface
80 }
81 return CallStatic
82 default:
83 return CallDynamic
84 }
85 }
86
87 // UsedIdent returns the identifier such that info.Uses[UsedIdent(info, e)]
88 // is the [types.Object] used by e, if any.
89 //
90 // If e is one of various forms of reference:
91 //
92 // f, c, v, T lexical reference
93 // pkg.X qualified identifier
94 // f[T] or pkg.F[K,V] instantiations of the above kinds
95 // expr.f field or method value selector
96 // T.f method expression selector
97 //
98 // UsedIdent returns the identifier whose is associated value in [types.Info.Uses]
99 // is the object to which it refers.
100 //
101 // For the declarations:
102 //
103 // func F[T any] {...}
104 // type I interface { M() }
105 // var (
106 // x int
107 // s struct { f int }
108 // a []int
109 // i I
110 // )
111 //
112 // UsedIdent returns the following:
113 //
114 // Expr UsedIdent
115 // x x
116 // s.f f
117 // F[int] F
118 // i.M M
119 // I.M M
120 // min min
121 // int int
122 // 1 nil
123 // a[0] nil
124 // []byte nil
125 //
126 // Note: if e is an instantiated function or method, UsedIdent returns
127 // the corresponding generic function or method on the generic type.
128 func UsedIdent(info *types.Info, e ast.Expr) *ast.Ident {
•129 if info.Types == nil || info.Uses == nil {
130 panic("one of info.Types or info.Uses is nil; both must be populated")
131 }
132 // Look through type instantiation if necessary.
133 switch d := ast.Unparen(e).(type) {
134 case *ast.IndexExpr:
135 if info.Types[d.Index].IsType() {
136 e = d.X
137 }
138 case *ast.IndexListExpr:
139 e = d.X
140 }
141
•142 switch e := ast.Unparen(e).(type) {
143 case *ast.Ident:
144 return e
145 case *ast.SelectorExpr:
146 return e.Sel
147 }
148 return nil
149 }
150
•151 func interfaceMethod(f *types.Func) bool {
152 recv := f.Signature().Recv()
153 return recv != nil && types.IsInterface(recv.Type())
154 }