/
expr.go
415 lines (401 loc) · 12.7 KB
/
expr.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Copyright (c) 2020, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package golang
import (
"fmt"
"os"
"path/filepath"
"strings"
"cogentcore.org/core/parse"
"cogentcore.org/core/parse/parser"
"cogentcore.org/core/parse/syms"
"cogentcore.org/core/parse/token"
)
// TypeFromAstExprStart starts walking the ast expression to find the type.
// This computes the last ast point as the stopping point for processing
// and then calls TypeFromAstExpr.
// It returns the type, any Ast node that remained unprocessed at the end, and bool if found.
func (gl *GoLang) TypeFromAstExprStart(fs *parse.FileState, origPkg, pkg *syms.Symbol, tyast *parser.Ast) (*syms.Type, *parser.Ast, bool) {
last := tyast.NextSiblingAst()
// fmt.Printf("last: %v \n", last.PathUnique())
return gl.TypeFromAstExpr(fs, origPkg, pkg, tyast, last)
}
// TypeFromAstExpr walks the ast expression to find the type.
// It returns the type, any Ast node that remained unprocessed at the end, and bool if found.
func (gl *GoLang) TypeFromAstExpr(fs *parse.FileState, origPkg, pkg *syms.Symbol, tyast, last *parser.Ast) (*syms.Type, *parser.Ast, bool) {
pos := tyast.SrcReg.St
fpath, _ := filepath.Abs(fs.Src.Filename)
// containers of given region -- local scoping
var conts syms.SymMap
fs.Syms.FindContainsRegion(fpath, pos, 2, token.NameFunction, &conts) // 2 extra lines always!
// if TraceTypes && len(conts) == 0 {
// fmt.Printf("TExpr: no conts for fpath: %v pos: %v\n", fpath, pos)
// }
// if TraceTypes {
// tyast.WriteTree(os.Stdout, 0)
// }
tnm := tyast.Nm
switch {
case tnm == "FuncCall":
fun := tyast.NextAst()
if fun == nil {
return nil, nil, false
}
funm := fun.Src
sym, got := fs.FindNameScoped(funm, conts)
if got {
if !gl.InferEmptySymbolType(sym, fs, pkg) {
return nil, fun, false
}
if sym.Type == "" {
if TraceTypes {
fmt.Printf("TExpr: FuncCall: function type not set yet: %v\n", funm)
}
gl.InferSymbolType(sym, fs, pkg, true)
}
ftnm := sym.Type
ftyp, _ := gl.FindTypeName(ftnm, fs, pkg)
if ftyp != nil && len(ftyp.Size) == 2 {
return gl.TypeFromFuncCall(fs, origPkg, pkg, tyast, last, ftyp)
}
if TraceTypes {
fmt.Printf("TExpr: FuncCall: could not find function: %v\n", funm)
}
return nil, fun, false
} else {
if funm == "len" || funm == "cap" {
return BuiltinTypes["int"], nil, true
}
if funm == "append" {
farg := fun.NextAst().NextAst()
return gl.TypeFromAstExpr(fs, origPkg, pkg, farg, last)
}
ctyp, _ := gl.FindTypeName(funm, fs, pkg) // conversion
if ctyp != nil {
return ctyp, nil, true
}
if TraceTypes {
fmt.Printf("TExpr: FuncCall: could not find function: %v\n", funm)
}
return nil, fun, false
}
case tnm == "Selector":
if tyast.NumChildren() == 0 { // incomplete
return nil, nil, false
}
tnmA := tyast.ChildAst(0)
if tnmA.Nm != "Name" {
if TraceTypes {
fmt.Printf("TExpr: selector start node kid is not a Name: %v, src: %v\n", tnmA.Nm, tnmA.Src)
tnmA.WriteTree(os.Stdout, 0)
}
return nil, tnmA, false
}
return gl.TypeFromAstName(fs, origPkg, pkg, tnmA, last, conts)
case tnm == "Slice": // strings.HasPrefix(tnm, "Slice"):
if tyast.NumChildren() == 0 { // incomplete
return nil, nil, false
}
tnmA := tyast.ChildAst(0)
if tnmA.Nm != "Name" {
if TraceTypes {
fmt.Printf("TExpr: slice start node kid is not a Name: %v, src: %v\n", tnmA.Nm, tnmA.Src)
}
return nil, tnmA, false
}
snm := tnmA.Src
sym, got := fs.FindNameScoped(snm, conts)
if got {
return gl.TypeFromAstSym(fs, origPkg, pkg, tnmA, last, sym)
}
if TraceTypes {
fmt.Printf("TExpr: could not find symbol for slice var name: %v\n", snm)
}
return nil, tnmA, false
case tnm == "Name":
return gl.TypeFromAstName(fs, origPkg, pkg, tyast, last, conts)
case strings.HasPrefix(tnm, "Lit"):
sty, got := gl.TypeFromAstLit(fs, pkg, nil, tyast)
return sty, nil, got
case strings.HasSuffix(tnm, "AutoType"):
sty, got := gl.SubTypeFromAst(fs, pkg, tyast, 0)
return sty, nil, got
case tnm == "CompositeLit":
sty, got := gl.SubTypeFromAst(fs, pkg, tyast, 0)
return sty, nil, got
case tnm == "AddrExpr":
if !tyast.HasChildren() {
return nil, nil, false
}
ch := tyast.ChildAst(0)
snm := tyast.Src[1:] // after &
var sty *syms.Type
switch ch.Nm {
case "CompositeLit":
sty, _ = gl.SubTypeFromAst(fs, pkg, ch, 0)
case "Selector":
sty, _ = gl.TypeFromAst(fs, pkg, nil, ch)
case "Name":
sym, got := fs.FindNameScoped(snm, conts)
if got {
sty, _, got = gl.TypeFromAstSym(fs, origPkg, pkg, ch, last, sym)
} else {
if snm == "true" || snm == "false" {
return BuiltinTypes["bool"], nil, true
}
if TraceTypes {
fmt.Printf("TExpr: could not find symbol named: %v\n", snm)
}
}
}
if sty != nil {
ty := &syms.Type{}
ty.Kind = syms.Ptr
tynm := SymTypeNameForPkg(sty, pkg)
ty.Name = "*" + tynm
ty.Els.Add("ptr", tynm)
return ty, nil, true
}
if TraceTypes {
fmt.Printf("TExpr: could not process addr expr:\n")
tyast.WriteTree(os.Stdout, 0)
}
return nil, tyast, false
case tnm == "DePtrExpr":
sty, got := gl.SubTypeFromAst(fs, pkg, tyast, 0) // first child
return sty, nil, got
case strings.HasSuffix(tnm, "Expr"):
// note: could figure out actual numerical type, but in practice we don't care
// for lookup / completion, so ignoring for now.
return BuiltinTypes["float64"], nil, true
case tnm == "TypeAssert":
sty, got := gl.SubTypeFromAst(fs, pkg, tyast, 1) // type is second child
return sty, nil, got
case tnm == "MakeCall":
sty, got := gl.SubTypeFromAst(fs, pkg, tyast, 0)
return sty, nil, got
case strings.Contains(tnm, "Chan"):
sty, got := gl.SubTypeFromAst(fs, pkg, tyast, 0)
return sty, nil, got
default:
if TraceTypes {
fmt.Printf("TExpr: cannot start with: %v\n", tyast.Nm)
tyast.WriteTree(os.Stdout, 0)
}
return nil, tyast, false
}
return nil, tyast, false
}
// TypeFromAstSym attempts to get the type from given symbol as part of expression.
// It returns the type, any Ast node that remained unprocessed at the end, and bool if found.
func (gl *GoLang) TypeFromAstSym(fs *parse.FileState, origPkg, pkg *syms.Symbol, tyast, last *parser.Ast, sym *syms.Symbol) (*syms.Type, *parser.Ast, bool) {
// if TraceTypes {
// fmt.Printf("TExpr: sym named: %v kind: %v type: %v\n", sym.Name, sym.Kind, sym.Type)
// }
if sym.Kind.SubCat() == token.NameScope {
// if TraceTypes {
// fmt.Printf("TExpr: symbol has scope type (package) -- will be looked up in a sec\n")
// }
return nil, nil, false // higher-level will catch it
}
if !gl.InferEmptySymbolType(sym, fs, pkg) {
return nil, tyast, false
}
tnm := sym.Type
return gl.TypeFromAstType(fs, origPkg, pkg, tyast, last, tnm)
}
// TypeFromAstType walks the ast expression to find the type, starting from current type name.
// It returns the type, any Ast node that remained unprocessed at the end, and bool if found.
func (gl *GoLang) TypeFromAstType(fs *parse.FileState, origPkg, pkg *syms.Symbol, tyast, last *parser.Ast, tnm string) (*syms.Type, *parser.Ast, bool) {
if tnm[0] == '*' {
tnm = tnm[1:]
}
ttp, npkg := gl.FindTypeName(tnm, fs, pkg)
if ttp == nil {
if TraceTypes {
fmt.Printf("TExpr: error -- couldn't find type name: %v\n", tnm)
}
return nil, tyast, false
}
pkgnm := ""
if pi := strings.Index(ttp.Name, "."); pi > 0 {
pkgnm = ttp.Name[:pi]
}
if npkg != origPkg { // need to make a package-qualified copy of type
if pkgnm == "" {
pkgnm = npkg.Name
qtnm := QualifyType(pkgnm, ttp.Name)
if qtnm != ttp.Name {
if etyp, ok := pkg.Types[qtnm]; ok {
ttp = etyp
} else {
ntyp := &syms.Type{}
*ntyp = *ttp
ntyp.Name = qtnm
origPkg.Types.Add(ntyp)
ttp = ntyp
}
}
}
}
pkg = npkg // update to new context
// if TraceTypes {
// fmt.Printf("TExpr: found type: %v kind: %v\n", ttp.Name, ttp.Kind)
// }
if tyast == nil || tyast == last {
return ttp, tyast, true
}
if tyast.Nm == "QualType" && tnm != tyast.Src {
// tyast.Src is new type name
return gl.TypeFromAstType(fs, origPkg, pkg, tyast, last, tyast.Src)
}
nxt := tyast
for {
nxt = nxt.NextAst()
if nxt == nil || nxt == last {
// if TraceTypes {
// fmt.Printf("TExpr: returning terminal type\n")
// }
return ttp, nxt, true
}
brk := false
switch {
case nxt.Nm == "Name":
brk = true
case strings.HasPrefix(nxt.Nm, "Lit"):
sty, got := gl.TypeFromAstLit(fs, pkg, nil, nxt)
return sty, nil, got
case nxt.Nm == "TypeAssert":
sty, got := gl.SubTypeFromAst(fs, origPkg, nxt, 1) // type is second child, switch back to orig pkg
return sty, nil, got
case nxt.Nm == "Slice":
continue
case strings.HasPrefix(nxt.Nm, "Slice"):
eltyp := ttp.Els.ByName("val")
if eltyp != nil {
elnm := QualifyType(pkgnm, eltyp.Type)
// if TraceTypes {
// fmt.Printf("TExpr: slice/map el type: %v\n", elnm)
// }
return gl.TypeFromAstType(fs, origPkg, pkg, nxt, last, elnm)
}
if ttp.Name == "string" {
return BuiltinTypes["string"], nil, true
}
if TraceTypes {
fmt.Printf("TExpr: slice operator not on slice: %v\n", ttp.Name)
tyast.WriteTree(os.Stdout, 0)
}
case nxt.Nm == "FuncCall":
// ttp is the function type name
fun := nxt.NextAst()
if fun == nil || fun == last {
return ttp, fun, true
}
funm := fun.Src
ftyp, got := ttp.Meths[funm]
if got && len(ftyp.Size) == 2 {
return gl.TypeFromFuncCall(fs, origPkg, pkg, nxt, last, ftyp)
} else {
if TraceTypes {
fmt.Printf("TExpr: FuncCall: could not find method: %v in type: %v\n", funm, ttp.Name)
tyast.WriteTree(os.Stdout, 0)
}
return nil, fun, false
}
}
if brk || nxt == nil || nxt == last {
break
}
// if TraceTypes {
// fmt.Printf("TExpr: skipping over %v\n", nxt.Nm)
// }
}
if nxt == nil {
return ttp, nxt, false
}
nm := nxt.Src
stp := ttp.Els.ByName(nm)
if stp != nil {
// if TraceTypes {
// fmt.Printf("TExpr: found Name: %v in type els\n", nm)
// }
return gl.TypeFromAstType(fs, origPkg, pkg, nxt, last, stp.Type)
}
// if TraceTypes {
// fmt.Printf("TExpr: error -- Name: %v not found in type els\n", nm)
// // ttp.WriteDoc(os.Stdout, 0)
// }
return ttp, nxt, true // robust, needed for completion
}
// TypeFromAstFuncCall gets return type of function call as return value, and returns the sibling node to
// continue parsing in, skipping over everything in the function call
func (gl *GoLang) TypeFromFuncCall(fs *parse.FileState, origPkg, pkg *syms.Symbol, tyast, last *parser.Ast, ftyp *syms.Type) (*syms.Type, *parser.Ast, bool) {
nxt := tyast.NextSiblingAst() // skip over everything within method in ast
if len(ftyp.Size) != 2 {
if TraceTypes {
fmt.Printf("TExpr: FuncCall: %v is not properly initialized with sizes\n", ftyp.Name)
}
return nil, nxt, false
}
npars := ftyp.Size[0] // first size is number of params
nrval := ftyp.Size[1] // second size is number of return values
if nrval == 0 {
if TraceTypes {
fmt.Printf("TExpr: FuncCall: %v has no return value\n", ftyp.Name)
}
return nil, nxt, false // no return -- shouldn't happen
}
rtyp := ftyp.Els[npars] // first return
if nxt != nil && nxt.Nm == "Name" { // direct de-ref on function return value -- AstType assumes nxt is type el
prv := nxt.PrevAst()
if prv != tyast {
nxt = prv
}
}
// if TraceTypes {
// fmt.Printf("got return type: %v\n", rtyp)
// }
return gl.TypeFromAstType(fs, origPkg, pkg, nxt, last, rtyp.Type)
}
// TypeFromAstName gets type from a Name in a given context (conts)
func (gl *GoLang) TypeFromAstName(fs *parse.FileState, origPkg, pkg *syms.Symbol, tyast, last *parser.Ast, conts syms.SymMap) (*syms.Type, *parser.Ast, bool) {
snm := tyast.Src
sym, got := fs.FindNameScoped(snm, conts)
if got && sym.Kind.SubCat() != token.NameScope {
tsym, nnxt, got := gl.TypeFromAstSym(fs, origPkg, pkg, tyast, last, sym)
if got {
return tsym, nnxt, got
}
if TraceTypes {
fmt.Printf("TExpr: got symbol but could not get type from sym name: %v\n", snm)
// tyast.WriteTree(os.Stdout, 0)
}
}
if snm == "true" || snm == "false" {
return BuiltinTypes["bool"], nil, true
}
// maybe it is a package name
psym, has := gl.PkgSyms(fs, pkg.Children, snm)
if has {
// if TraceTypes {
// fmt.Printf("TExpr: entering package name: %v\n", snm)
// }
nxt := tyast.NextAst()
if nxt != nil {
if nxt.Nm == "Selector" {
nxt = nxt.NextAst()
}
return gl.TypeFromAstExpr(fs, origPkg, psym, nxt, last)
}
if TraceTypes {
fmt.Printf("TExpr: package alone not useful\n")
}
return nil, tyast, false // package alone not useful
}
if TraceTypes {
fmt.Printf("TExpr: could not find symbol for name: %v\n", snm)
}
return nil, tyast, false
}