Debian Codegraph @

refactor/eg/match.go

raw symbols SCIP
1 // Copyright 2014 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 eg
6
7 import (
8 "fmt"
9 "go/ast"
10 "go/constant"
11 "go/token"
12 "go/types"
13 "log"
14 "os"
15 "reflect"
16 )
17
18 // matchExpr reports whether pattern x matches y.
19 //
20 // If tr.allowWildcards, Idents in x that refer to parameters are
21 // treated as wildcards, and match any y that is assignable to the
22 // parameter type; matchExpr records this correspondence in tr.env.
23 // Otherwise, matchExpr simply reports whether the two trees are
24 // equivalent.
25 //
26 // A wildcard appearing more than once in the pattern must
27 // consistently match the same tree.
28 func (tr *Transformer) matchExpr(x, y ast.Expr) bool {
29 if x == nil && y == nil {
30 return true
31 }
32 if x == nil || y == nil {
33 return false
34 }
35 x = ast.Unparen(x)
36 y = ast.Unparen(y)
37
38 // Is x a wildcard? (a reference to a 'before' parameter)
39 if xobj, ok := tr.wildcardObj(x); ok {
40 return tr.matchWildcard(xobj, y)
41 }
42
43 // Object identifiers (including pkg-qualified ones)
44 // are handled semantically, not syntactically.
45 xobj := isRef(x, tr.info)
46 yobj := isRef(y, tr.info)
47 if xobj != nil {
48 return xobj == yobj
49 }
50 if yobj != nil {
51 return false
52 }
53
54 // TODO(adonovan): audit: we cannot assume these ast.Exprs
55 // contain non-nil pointers. e.g. ImportSpec.Name may be a
56 // nil *ast.Ident.
57
58 if reflect.TypeOf(x) != reflect.TypeOf(y) {
59 return false
60 }
61 switch x := x.(type) {
62 case *ast.Ident:
63 log.Fatalf("unexpected Ident: %s", astString(tr.fset, x))
64
65 case *ast.BasicLit:
66 y := y.(*ast.BasicLit)
67 xval := constant.MakeFromLiteral(x.Value, x.Kind, 0)
68 yval := constant.MakeFromLiteral(y.Value, y.Kind, 0)
69 return constant.Compare(xval, token.EQL, yval)
70
71 case *ast.FuncLit:
72 // func literals (and thus statement syntax) never match.
73 return false
74
75 case *ast.CompositeLit:
76 y := y.(*ast.CompositeLit)
77 return (x.Type == nil) == (y.Type == nil) &&
78 (x.Type == nil || tr.matchType(x.Type, y.Type)) &&
79 tr.matchExprs(x.Elts, y.Elts)
80
81 case *ast.SelectorExpr:
82 y := y.(*ast.SelectorExpr)
83 return tr.matchSelectorExpr(x, y) &&
84 tr.info.Selections[x].Obj() == tr.info.Selections[y].Obj()
85
86 case *ast.IndexExpr:
87 y := y.(*ast.IndexExpr)
88 return tr.matchExpr(x.X, y.X) &&
89 tr.matchExpr(x.Index, y.Index)
90
91 case *ast.SliceExpr:
92 y := y.(*ast.SliceExpr)
93 return tr.matchExpr(x.X, y.X) &&
94 tr.matchExpr(x.Low, y.Low) &&
95 tr.matchExpr(x.High, y.High) &&
96 tr.matchExpr(x.Max, y.Max) &&
97 x.Slice3 == y.Slice3
98
99 case *ast.TypeAssertExpr:
100 y := y.(*ast.TypeAssertExpr)
101 return tr.matchExpr(x.X, y.X) &&
102 tr.matchType(x.Type, y.Type)
103
104 case *ast.CallExpr:
105 y := y.(*ast.CallExpr)
106 match := tr.matchExpr // function call
107 if tr.info.Types[x.Fun].IsType() {
108 match = tr.matchType // type conversion
109 }
110 return x.Ellipsis.IsValid() == y.Ellipsis.IsValid() &&
111 match(x.Fun, y.Fun) &&
112 tr.matchExprs(x.Args, y.Args)
113
114 case *ast.StarExpr:
115 y := y.(*ast.StarExpr)
116 return tr.matchExpr(x.X, y.X)
117
118 case *ast.UnaryExpr:
119 y := y.(*ast.UnaryExpr)
120 return x.Op == y.Op &&
121 tr.matchExpr(x.X, y.X)
122
123 case *ast.BinaryExpr:
124 y := y.(*ast.BinaryExpr)
125 return x.Op == y.Op &&
126 tr.matchExpr(x.X, y.X) &&
127 tr.matchExpr(x.Y, y.Y)
128
129 case *ast.KeyValueExpr:
130 y := y.(*ast.KeyValueExpr)
131 return tr.matchExpr(x.Key, y.Key) &&
132 tr.matchExpr(x.Value, y.Value)
133 }
134
135 panic(fmt.Sprintf("unhandled AST node type: %T", x))
136 }
137
138 func (tr *Transformer) matchExprs(xx, yy []ast.Expr) bool {
139 if len(xx) != len(yy) {
140 return false
141 }
142 for i := range xx {
143 if !tr.matchExpr(xx[i], yy[i]) {
144 return false
145 }
146 }
147 return true
148 }
149
150 // matchType reports whether the two type ASTs denote identical types.
151 func (tr *Transformer) matchType(x, y ast.Expr) bool {
152 tx := tr.info.Types[x].Type
153 ty := tr.info.Types[y].Type
154 return types.Identical(tx, ty)
155 }
156
157 func (tr *Transformer) wildcardObj(x ast.Expr) (*types.Var, bool) {
158 if x, ok := x.(*ast.Ident); ok && x != nil && tr.allowWildcards {
159 if xobj, ok := tr.info.Uses[x].(*types.Var); ok && tr.wildcards[xobj] {
160 return xobj, true
161 }
162 }
163 return nil, false
164 }
165
166 func (tr *Transformer) matchSelectorExpr(x, y *ast.SelectorExpr) bool {
167 if xobj, ok := tr.wildcardObj(x.X); ok {
168 field := x.Sel.Name
169 yt := tr.info.TypeOf(y.X)
170 o, _, _ := types.LookupFieldOrMethod(yt, true, tr.currentPkg, field)
171 if o != nil {
172 tr.env[xobj.Name()] = y.X // record binding
173 return true
174 }
175 }
176 return tr.matchExpr(x.X, y.X)
177 }
178
179 func (tr *Transformer) matchWildcard(xobj *types.Var, y ast.Expr) bool {
180 name := xobj.Name()
181
182 if tr.verbose {
183 fmt.Fprintf(os.Stderr, "%s: wildcard %s -> %s?: ",
184 tr.fset.Position(y.Pos()), name, astString(tr.fset, y))
185 }
186
187 // Check that y is assignable to the declared type of the param.
188 yt := tr.info.TypeOf(y)
189 if yt == nil {
190 // y has no type.
191 // Perhaps it is an *ast.Ellipsis in [...]T{}, or
192 // an *ast.KeyValueExpr in T{k: v}.
193 // Clearly these pseudo-expressions cannot match a
194 // wildcard, but it would nice if we had a way to ignore
195 // the difference between T{v} and T{k:v} for structs.
196 return false
197 }
198 if !types.AssignableTo(yt, xobj.Type()) {
199 if tr.verbose {
200 fmt.Fprintf(os.Stderr, "%s not assignable to %s\n", yt, xobj.Type())
201 }
202 return false
203 }
204
205 // A wildcard matches any expression.
206 // If it appears multiple times in the pattern, it must match
207 // the same expression each time.
208 if old, ok := tr.env[name]; ok {
209 // found existing binding
210 tr.allowWildcards = false
211 r := tr.matchExpr(old, y)
212 if tr.verbose {
213 fmt.Fprintf(os.Stderr, "%t secondary match, primary was %s\n",
214 r, astString(tr.fset, old))
215 }
216 tr.allowWildcards = true
217 return r
218 }
219
220 if tr.verbose {
221 fmt.Fprintf(os.Stderr, "primary match\n")
222 }
223
224 tr.env[name] = y // record binding
225 return true
226 }
227
228 // -- utilities --------------------------------------------------------
229
230 // isRef returns the object referred to by this (possibly qualified)
231 // identifier, or nil if the node is not a referring identifier.
232 func isRef(n ast.Node, info *types.Info) types.Object {
233 switch n := n.(type) {
234 case *ast.Ident:
235 return info.Uses[n]
236
237 case *ast.SelectorExpr:
238 if _, ok := info.Selections[n]; !ok {
239 // qualified ident
240 return info.Uses[n.Sel]
241 }
242 }
243 return nil
244 }