Debian Codegraph @

interp/value.go

raw symbols SCIP
1 package interp
2
3 import (
4 "go/constant"
5 "reflect"
6 )
7
8 const (
9 notInFrame = -1 // value of node.findex for literal values (not in frame)
10 globalFrame = -1 // value of node.level for global symbols
11 )
12
13 func valueGenerator(n *node, i int) func(*frame) reflect.Value {
14 switch n.level {
15 case globalFrame:
16 return func(f *frame) reflect.Value { return valueOf(f.root.data, i) }
17 case 0:
18 return func(f *frame) reflect.Value { return valueOf(f.data, i) }
19 case 1:
20 return func(f *frame) reflect.Value { return valueOf(f.anc.data, i) }
21 case 2:
22 return func(f *frame) reflect.Value { return valueOf(f.anc.anc.data, i) }
23 default:
24 return func(f *frame) reflect.Value {
25 for level := n.level; level > 0; level-- {
26 f = f.anc
27 }
28 return valueOf(f.data, i)
29 }
30 }
31 }
32
33 // valueOf safely recovers the ith element of data. This is necessary
34 // because a cancellation prior to any evaluation result may leave
35 // the frame's data empty.
36 func valueOf(data []reflect.Value, i int) reflect.Value {
37 if i < 0 || i >= len(data) {
38 return reflect.Value{}
39 }
40 return data[i]
41 }
42
43 func genValueRecv(n *node) func(*frame) reflect.Value {
44 var v func(*frame) reflect.Value
45 if n.recv.node == nil {
46 v = func(*frame) reflect.Value { return n.recv.val }
47 } else {
48 v = genValue(n.recv.node)
49 }
50 fi := n.recv.index
51
52 if len(fi) == 0 {
53 return v
54 }
55
56 return func(f *frame) reflect.Value {
57 r := v(f)
58 for _, i := range fi {
59 if r.Kind() == reflect.Ptr {
60 r = r.Elem()
61 }
62 // Note that we can't use reflect FieldByIndex method, as we may
63 // traverse valueInterface wrappers to access the embedded receiver.
64 r = r.Field(i)
65 vi, ok := r.Interface().(valueInterface)
66 if ok {
67 r = vi.value
68 }
69 }
70 return r
71 }
72 }
73
74 func genValueAsFunctionWrapper(n *node) func(*frame) reflect.Value {
75 value := genValue(n)
76 typ := n.typ.TypeOf()
77
78 return func(f *frame) reflect.Value {
79 v := value(f)
80 if v.IsNil() {
81 return reflect.New(typ).Elem()
82 }
83 if v.Kind() == reflect.Func {
84 return v
85 }
86 vn, ok := v.Interface().(*node)
87 if ok && vn.rval.Kind() == reflect.Func {
88 // The node value is already a callable func, no need to wrap it.
89 return vn.rval
90 }
91 return genFunctionWrapper(vn)(f)
92 }
93 }
94
95 func genValueAs(n *node, t reflect.Type) func(*frame) reflect.Value {
96 value := genValue(n)
97
98 return func(f *frame) reflect.Value {
99 v := value(f)
100 switch v.Kind() {
101 case reflect.Chan, reflect.Func, reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice, reflect.UnsafePointer:
102 if v.IsNil() {
103 return reflect.New(t).Elem()
104 }
105 }
106 return v.Convert(t)
107 }
108 }
109
110 func genValue(n *node) func(*frame) reflect.Value {
111 switch n.kind {
112 case basicLit:
113 convertConstantValue(n)
114 v := n.rval
115 if !v.IsValid() {
116 v = reflect.New(emptyInterfaceType).Elem()
117 }
118 return func(f *frame) reflect.Value { return v }
119 case funcDecl:
120 var v reflect.Value
121 if w, ok := n.val.(reflect.Value); ok {
122 v = w
123 } else {
124 v = reflect.ValueOf(n.val)
125 }
126 return func(f *frame) reflect.Value { return v }
127 default:
128 if n.rval.IsValid() {
129 convertConstantValue(n)
130 v := n.rval
131 return func(f *frame) reflect.Value { return v }
132 }
133 if n.sym != nil {
134 i := n.sym.index
135 if i < 0 && n != n.sym.node {
136 return genValue(n.sym.node)
137 }
138 if n.sym.global {
139 return func(f *frame) reflect.Value { return f.root.data[i] }
140 }
141 return valueGenerator(n, i)
142 }
143 if n.findex == notInFrame {
144 var v reflect.Value
145 if w, ok := n.val.(reflect.Value); ok {
146 v = w
147 } else {
148 v = reflect.ValueOf(n.val)
149 }
150 return func(f *frame) reflect.Value { return v }
151 }
152 return valueGenerator(n, n.findex)
153 }
154 }
155
156 func genDestValue(typ *itype, n *node) func(*frame) reflect.Value {
157 convertLiteralValue(n, typ.TypeOf())
158 switch {
159 case isInterfaceSrc(typ) && (!isEmptyInterface(typ) || len(n.typ.method) > 0):
160 return genValueInterface(n)
161 case isNamedFuncSrc(n.typ):
162 return genFunctionWrapper(n)
163 case isInterfaceBin(typ):
164 return genInterfaceWrapper(n, typ.rtype)
165 case n.kind == basicLit && n.val == nil:
166 return func(*frame) reflect.Value { return reflect.New(typ.rtype).Elem() }
167 case n.typ.untyped && isComplex(typ.TypeOf()):
168 return genValueComplex(n)
169 case n.typ.untyped && !typ.untyped:
170 return genValueAs(n, typ.TypeOf())
171 }
172 return genValue(n)
173 }
174
175 func genFuncValue(n *node) func(*frame) reflect.Value {
176 value := genValue(n)
177 return func(f *frame) reflect.Value {
178 v := value(f)
179 if nod, ok := v.Interface().(*node); ok {
180 return genFunctionWrapper(nod)(f)
181 }
182 return v
183 }
184 }
185
186 func genValueArray(n *node) func(*frame) reflect.Value {
187 value := genValue(n)
188 // dereference array pointer, to support array operations on array pointer
189 if n.typ.TypeOf().Kind() == reflect.Ptr {
190 return func(f *frame) reflect.Value {
191 return value(f).Elem()
192 }
193 }
194 return value
195 }
196
197 func genValueRangeArray(n *node) func(*frame) reflect.Value {
198 value := genValue(n)
199
200 switch {
201 case n.typ.TypeOf().Kind() == reflect.Ptr:
202 // dereference array pointer, to support array operations on array pointer
203 return func(f *frame) reflect.Value {
204 return value(f).Elem()
205 }
206 case n.typ.val != nil && n.typ.val.cat == interfaceT:
207 if len(n.typ.val.field) > 0 {
208 return func(f *frame) reflect.Value {
209 val := value(f)
210 v := []valueInterface{}
211 for i := 0; i < val.Len(); i++ {
212 switch av := val.Index(i).Interface().(type) {
213 case []valueInterface:
214 v = append(v, av...)
215 case valueInterface:
216 v = append(v, av)
217 default:
218 panic(n.cfgErrorf("invalid type %v", val.Index(i).Type()))
219 }
220 }
221 return reflect.ValueOf(v)
222 }
223 }
224 // empty interface, do not wrap.
225 fallthrough
226 default:
227 return func(f *frame) reflect.Value {
228 // This is necessary to prevent changes in the returned
229 // reflect.Value being reflected back to the value used
230 // for the range expression.
231 return reflect.ValueOf(value(f).Interface())
232 }
233 }
234 }
235
236 func genValueInterface(n *node) func(*frame) reflect.Value {
237 value := genValue(n)
238
239 return func(f *frame) reflect.Value {
240 v := value(f)
241 nod := n
242
243 for v.IsValid() {
244 // traverse interface indirections to find out concrete type
245 vi, ok := v.Interface().(valueInterface)
246 if !ok {
247 break
248 }
249 v = vi.value
250 nod = vi.node
251 }
252
253 // empty interface, do not wrap.
254 if nod != nil && isEmptyInterface(nod.typ) {
255 return v
256 }
257
258 return reflect.ValueOf(valueInterface{nod, v})
259 }
260 }
261
262 func getConcreteValue(val reflect.Value) reflect.Value {
263 v := val
264 for {
265 vi, ok := v.Interface().(valueInterface)
266 if !ok {
267 break
268 }
269 v = vi.value
270 }
271 if v.NumMethod() > 0 {
272 return v
273 }
274 if v.Kind() != reflect.Struct {
275 return v
276 }
277 // Search a concrete value in fields of an emulated interface.
278 for i := v.NumField() - 1; i >= 0; i-- {
279 vv := v.Field(i)
280 if vv.Kind() == reflect.Interface {
281 vv = vv.Elem()
282 }
283 if vv.IsValid() {
284 return vv
285 }
286 }
287 return v
288 }
289
290 func zeroInterfaceValue() reflect.Value {
291 n := &node{kind: basicLit, typ: &itype{cat: nilT, untyped: true, str: "nil"}}
292 v := reflect.New(emptyInterfaceType).Elem()
293 return reflect.ValueOf(valueInterface{n, v})
294 }
295
296 func wantEmptyInterface(n *node) bool {
297 return isEmptyInterface(n.typ) ||
298 n.anc.action == aAssign && n.anc.typ.cat == interfaceT && len(n.anc.typ.field) == 0 ||
299 n.anc.kind == returnStmt && n.anc.val.(*node).typ.ret[0].cat == interfaceT && len(n.anc.val.(*node).typ.ret[0].field) == 0
300 }
301
302 func genValueOutput(n *node, t reflect.Type) func(*frame) reflect.Value {
303 value := genValue(n)
304 switch {
305 case n.anc.action == aAssign && n.anc.typ.cat == interfaceT:
306 if len(n.anc.typ.field) == 0 {
307 // empty interface, do not wrap
308 return value
309 }
310 fallthrough
311 case n.anc.kind == returnStmt && n.anc.val.(*node).typ.ret[0].cat == interfaceT:
312 if nod, ok := n.anc.val.(*node); !ok || len(nod.typ.ret[0].field) == 0 {
313 // empty interface, do not wrap
314 return value
315 }
316 // The result of the builtin has to be returned as an interface type.
317 // Wrap it in a valueInterface and return the dereferenced value.
318 return func(f *frame) reflect.Value {
319 d := value(f)
320 v := reflect.New(t).Elem()
321 d.Set(reflect.ValueOf(valueInterface{n, v}))
322 return v
323 }
324 }
325 return value
326 }
327
328 func getBinValue(getMapType func(*itype) reflect.Type, value func(*frame) reflect.Value, f *frame) reflect.Value {
329 v := value(f)
330 if getMapType == nil {
331 return v
332 }
333 val, ok := v.Interface().(valueInterface)
334 if !ok || val.node == nil {
335 return v
336 }
337 if rt := getMapType(val.node.typ); rt != nil {
338 return genInterfaceWrapper(val.node, rt)(f)
339 }
340 return v
341 }
342
343 func valueInterfaceValue(v reflect.Value) reflect.Value {
344 for {
345 vv, ok := v.Interface().(valueInterface)
346 if !ok {
347 break
348 }
349 v = vv.value
350 }
351 return v
352 }
353
354 func genValueInterfaceValue(n *node) func(*frame) reflect.Value {
355 value := genValue(n)
356
357 return func(f *frame) reflect.Value {
358 v := value(f)
359 if vi, ok := v.Interface().(valueInterface); ok && vi.node == nil {
360 // Uninitialized interface value, set it to a correct zero value.
361 v.Set(zeroInterfaceValue())
362 v = value(f)
363 }
364 return valueInterfaceValue(v)
365 }
366 }
367
368 func vInt(v reflect.Value) (i int64) {
369 if c := vConstantValue(v); c != nil {
370 i, _ = constant.Int64Val(constant.ToInt(c))
371 return i
372 }
373 switch v.Kind() {
374 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
375 i = v.Int()
376 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
377 i = int64(v.Uint())
378 case reflect.Float32, reflect.Float64:
379 i = int64(v.Float())
380 case reflect.Complex64, reflect.Complex128:
381 i = int64(real(v.Complex()))
382 }
383 return
384 }
385
386 func vUint(v reflect.Value) (i uint64) {
387 if c := vConstantValue(v); c != nil {
388 i, _ = constant.Uint64Val(constant.ToInt(c))
389 return i
390 }
391 switch v.Kind() {
392 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
393 i = uint64(v.Int())
394 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
395 i = v.Uint()
396 case reflect.Float32, reflect.Float64:
397 i = uint64(v.Float())
398 case reflect.Complex64, reflect.Complex128:
399 i = uint64(real(v.Complex()))
400 }
401 return
402 }
403
404 func vComplex(v reflect.Value) (c complex128) {
405 if c := vConstantValue(v); c != nil {
406 c = constant.ToComplex(c)
407 rel, _ := constant.Float64Val(constant.Real(c))
408 img, _ := constant.Float64Val(constant.Imag(c))
409 return complex(rel, img)
410 }
411 switch v.Kind() {
412 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
413 c = complex(float64(v.Int()), 0)
414 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
415 c = complex(float64(v.Uint()), 0)
416 case reflect.Float32, reflect.Float64:
417 c = complex(v.Float(), 0)
418 case reflect.Complex64, reflect.Complex128:
419 c = v.Complex()
420 }
421 return
422 }
423
424 func vFloat(v reflect.Value) (i float64) {
425 if c := vConstantValue(v); c != nil {
426 i, _ = constant.Float64Val(constant.ToFloat(c))
427 return i
428 }
429 switch v.Kind() {
430 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
431 i = float64(v.Int())
432 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
433 i = float64(v.Uint())
434 case reflect.Float32, reflect.Float64:
435 i = v.Float()
436 case reflect.Complex64, reflect.Complex128:
437 i = real(v.Complex())
438 }
439 return
440 }
441
442 func vString(v reflect.Value) (s string) {
443 if c := vConstantValue(v); c != nil {
444 s = constant.StringVal(c)
445 return s
446 }
447 return v.String()
448 }
449
450 func vConstantValue(v reflect.Value) (c constant.Value) {
451 if v.Type().Implements(constVal) {
452 c = v.Interface().(constant.Value)
453 }
454 return
455 }
456
457 func genValueInt(n *node) func(*frame) (reflect.Value, int64) {
458 value := genValue(n)
459
460 switch n.typ.TypeOf().Kind() {
461 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
462 return func(f *frame) (reflect.Value, int64) { v := value(f); return v, v.Int() }
463 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
464 return func(f *frame) (reflect.Value, int64) { v := value(f); return v, int64(v.Uint()) }
465 case reflect.Float32, reflect.Float64:
466 return func(f *frame) (reflect.Value, int64) { v := value(f); return v, int64(v.Float()) }
467 case reflect.Complex64, reflect.Complex128:
468 if n.typ.untyped && n.rval.IsValid() && imag(n.rval.Complex()) == 0 {
469 return func(f *frame) (reflect.Value, int64) { v := value(f); return v, int64(real(v.Complex())) }
470 }
471 }
472 return nil
473 }
474
475 func genValueUint(n *node) func(*frame) (reflect.Value, uint64) {
476 value := genValue(n)
477
478 switch n.typ.TypeOf().Kind() {
479 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
480 return func(f *frame) (reflect.Value, uint64) { v := value(f); return v, uint64(v.Int()) }
481 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
482 return func(f *frame) (reflect.Value, uint64) { v := value(f); return v, v.Uint() }
483 case reflect.Float32, reflect.Float64:
484 return func(f *frame) (reflect.Value, uint64) { v := value(f); return v, uint64(v.Float()) }
485 case reflect.Complex64, reflect.Complex128:
486 if n.typ.untyped && n.rval.IsValid() && imag(n.rval.Complex()) == 0 {
487 return func(f *frame) (reflect.Value, uint64) { v := value(f); return v, uint64(real(v.Complex())) }
488 }
489 }
490 return nil
491 }
492
493 func genValueFloat(n *node) func(*frame) (reflect.Value, float64) {
494 value := genValue(n)
495
496 switch n.typ.TypeOf().Kind() {
497 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
498 return func(f *frame) (reflect.Value, float64) { v := value(f); return v, float64(v.Int()) }
499 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
500 return func(f *frame) (reflect.Value, float64) { v := value(f); return v, float64(v.Uint()) }
501 case reflect.Float32, reflect.Float64:
502 return func(f *frame) (reflect.Value, float64) { v := value(f); return v, v.Float() }
503 case reflect.Complex64, reflect.Complex128:
504 if n.typ.untyped && n.rval.IsValid() && imag(n.rval.Complex()) == 0 {
505 return func(f *frame) (reflect.Value, float64) { v := value(f); return v, real(v.Complex()) }
506 }
507 }
508 return nil
509 }
510
511 func genValueComplex(n *node) func(*frame) reflect.Value {
512 vc := genComplex(n)
513 return func(f *frame) reflect.Value { return reflect.ValueOf(vc(f)) }
514 }
515
516 func genComplex(n *node) func(*frame) complex128 {
517 value := genValue(n)
518
519 switch n.typ.TypeOf().Kind() {
520 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
521 return func(f *frame) complex128 { return complex(float64(value(f).Int()), 0) }
522 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
523 return func(f *frame) complex128 { return complex(float64(value(f).Uint()), 0) }
524 case reflect.Float32, reflect.Float64:
525 return func(f *frame) complex128 { return complex(value(f).Float(), 0) }
526 case reflect.Complex64, reflect.Complex128:
527 return func(f *frame) complex128 { return value(f).Complex() }
528 }
529 return nil
530 }
531
532 func genValueString(n *node) func(*frame) (reflect.Value, string) {
533 value := genValue(n)
534
535 return func(f *frame) (reflect.Value, string) { v := value(f); return v, v.String() }
536 }