-
Notifications
You must be signed in to change notification settings - Fork 323
/
resolver_go1.16.go
158 lines (128 loc) · 3.33 KB
/
resolver_go1.16.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//go:build go1.16
// +build go1.16
package names
import (
"go/ast"
)
type go116Resolver struct{}
func init() {
registerLanguageLevelResolver(&go116Resolver{})
}
func (g *go116Resolver) LanguageVersion() string {
return "1.16"
}
func (g *go116Resolver) expr(r *resolver, expr ast.Expr) (ok bool) {
switch expr := expr.(type) {
case *ast.Ident:
r.ident(expr)
case *ast.Ellipsis:
r.expr(expr.Elt)
case *ast.FuncLit:
r.openScope()
defer r.closeScope()
// First resolve types before introducing names
for _, param := range expr.Type.Params.List {
r.expr(param.Type)
}
if expr.Type.Results != nil {
for _, result := range expr.Type.Results.List {
r.expr(result.Type)
}
}
for _, field := range expr.Type.Params.List {
for _, name := range field.Names {
r.define(name, &Name{Local: true})
}
}
if expr.Type.Results != nil {
for _, field := range expr.Type.Results.List {
for _, name := range field.Names {
r.define(name, &Name{Local: true})
}
}
}
if expr.Body != nil {
r.stmt(expr.Body)
}
case *ast.CompositeLit:
r.expr(expr.Type)
r.exprList(expr.Elts)
case *ast.ParenExpr:
r.expr(expr.X)
case *ast.SelectorExpr:
r.expr(expr.X)
// Note: we don't treat 'Foo' in 'x.Foo' as an identifier,
// as it does not introduce a new name to any scope.
case *ast.IndexExpr:
r.expr(expr.X)
r.expr(expr.Index)
case *ast.SliceExpr:
r.expr(expr.X)
r.expr(expr.Low)
r.expr(expr.High)
r.expr(expr.Max)
case *ast.TypeAssertExpr:
r.expr(expr.X)
r.expr(expr.Type)
case *ast.CallExpr:
r.Calls = append(r.Calls, expr)
r.expr(expr.Fun)
r.exprList(expr.Args)
case *ast.StarExpr:
r.expr(expr.X)
case *ast.UnaryExpr:
r.expr(expr.X)
case *ast.BinaryExpr:
r.expr(expr.X)
r.expr(expr.Y)
case *ast.KeyValueExpr:
// HACK: We want to track uses of functions. This is tricky because
// struct types use keys that are idents that refer to the struct field,
// while map types can use keys to refer to idents in scope.
//
// Unfortunately We cannot easily know the type of the composite literal
// without typechecking. However, funcs are incomparable and therefore
// are not valid as map keys. So let's simply avoid tracking idents
// in the keys, and rely on the compiler to eventually catch this for us.
if _, ok := expr.Key.(*ast.Ident); !ok {
r.expr(expr.Key)
}
r.expr(expr.Value)
case *ast.ArrayType:
r.expr(expr.Len)
r.expr(expr.Elt)
case *ast.StructType:
for _, field := range expr.Fields.List {
r.expr(field.Type)
// Don't look at names; they don't resolve to outside scope
}
case *ast.FuncType:
for _, field := range expr.Params.List {
r.expr(field.Type)
// Don't look at names; they don't resolve to outside scope
}
if expr.Results != nil {
for _, field := range expr.Results.List {
r.expr(field.Type)
// Don't look at names; they don't resolve to outside scope
}
}
case *ast.InterfaceType:
for _, field := range expr.Methods.List {
r.expr(field.Type)
// Don't look at names; they don't resolve to outside scope
}
case *ast.MapType:
r.expr(expr.Key)
r.expr(expr.Value)
case *ast.ChanType:
r.expr(expr.Value)
case *ast.BadExpr, *ast.BasicLit:
// do nothing
default:
// If we don't process this then return false
return false
}
// Otherwise we processed it, so all ok
return true
}