github.com/traefik/yaegi@v0.15.1/interp/run.go (about) 1 package interp 2 3 //go:generate go run ../internal/cmd/genop/genop.go 4 5 import ( 6 "errors" 7 "fmt" 8 "go/constant" 9 "reflect" 10 "regexp" 11 "strings" 12 ) 13 14 // bltn type defines functions which run at CFG execution. 15 type bltn func(f *frame) bltn 16 17 // bltnGenerator type defines a builtin generator function. 18 type bltnGenerator func(n *node) 19 20 var builtin = [...]bltnGenerator{ 21 aNop: nop, 22 aAddr: addr, 23 aAssign: assign, 24 aAdd: add, 25 aAddAssign: addAssign, 26 aAnd: and, 27 aAndAssign: andAssign, 28 aAndNot: andNot, 29 aAndNotAssign: andNotAssign, 30 aBitNot: bitNot, 31 aCall: call, 32 aCallSlice: call, 33 aCase: _case, 34 aCompositeLit: arrayLit, 35 aDec: dec, 36 aEqual: equal, 37 aGetFunc: getFunc, 38 aGreater: greater, 39 aGreaterEqual: greaterEqual, 40 aInc: inc, 41 aLand: land, 42 aLor: lor, 43 aLower: lower, 44 aLowerEqual: lowerEqual, 45 aMul: mul, 46 aMulAssign: mulAssign, 47 aNeg: neg, 48 aNot: not, 49 aNotEqual: notEqual, 50 aOr: or, 51 aOrAssign: orAssign, 52 aPos: pos, 53 aQuo: quo, 54 aQuoAssign: quoAssign, 55 aRange: _range, 56 aRecv: recv, 57 aRem: rem, 58 aRemAssign: remAssign, 59 aReturn: _return, 60 aSend: send, 61 aShl: shl, 62 aShlAssign: shlAssign, 63 aShr: shr, 64 aShrAssign: shrAssign, 65 aSlice: slice, 66 aSlice0: slice0, 67 aStar: deref, 68 aSub: sub, 69 aSubAssign: subAssign, 70 aTypeAssert: typeAssertShort, 71 aXor: xor, 72 aXorAssign: xorAssign, 73 } 74 75 var receiverStripperRxp *regexp.Regexp 76 77 func init() { 78 re := `func\(((.*?(, |\)))(.*))` 79 var err error 80 receiverStripperRxp, err = regexp.Compile(re) 81 if err != nil { 82 panic(err) 83 } 84 } 85 86 type valueInterface struct { 87 node *node 88 value reflect.Value 89 } 90 91 var floatType, complexType reflect.Type 92 93 func init() { 94 floatType = reflect.ValueOf(0.0).Type() 95 complexType = reflect.ValueOf(complex(0, 0)).Type() 96 } 97 98 func (interp *Interpreter) run(n *node, cf *frame) { 99 if n == nil { 100 return 101 } 102 var f *frame 103 if cf == nil { 104 f = interp.frame 105 } else { 106 f = newFrame(cf, len(n.types), interp.runid()) 107 } 108 interp.mutex.RLock() 109 c := reflect.ValueOf(interp.done) 110 interp.mutex.RUnlock() 111 112 f.mutex.Lock() 113 f.done = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: c} 114 f.mutex.Unlock() 115 116 for i, t := range n.types { 117 f.data[i] = reflect.New(t).Elem() 118 } 119 runCfg(n.start, f, n, nil) 120 } 121 122 func isExecNode(n *node, exec bltn) bool { 123 if n == nil || n.exec == nil || exec == nil { 124 return false 125 } 126 127 a1 := reflect.ValueOf(n.exec).Pointer() 128 a2 := reflect.ValueOf(exec).Pointer() 129 return a1 == a2 130 } 131 132 // originalExecNode looks in the tree of nodes for the node which has exec, 133 // aside from n, in order to know where n "inherited" that exec from. 134 func originalExecNode(n *node, exec bltn) *node { 135 execAddr := reflect.ValueOf(exec).Pointer() 136 var originalNode *node 137 seen := make(map[int64]struct{}) 138 root := n 139 for { 140 root = root.anc 141 if root == nil { 142 break 143 } 144 if _, ok := seen[root.index]; ok { 145 continue 146 } 147 148 root.Walk(func(wn *node) bool { 149 if _, ok := seen[wn.index]; ok { 150 return true 151 } 152 seen[wn.index] = struct{}{} 153 if wn.index == n.index { 154 return true 155 } 156 if wn.exec == nil { 157 return true 158 } 159 if reflect.ValueOf(wn.exec).Pointer() == execAddr { 160 originalNode = wn 161 return false 162 } 163 return true 164 }, nil) 165 166 if originalNode != nil { 167 break 168 } 169 } 170 171 return originalNode 172 } 173 174 // cloned from net/http/server.go , so we can enforce a similar behavior: 175 // in the stdlib, this error is used as sentinel in panic triggered e.g. on 176 // request cancellation, in order to catch it and suppress it in a following defer. 177 // in yaegi, we use it to suppress a "panic" log message that happens in the 178 // same circumstances. 179 var errAbortHandler = errors.New("net/http: abort Handler") 180 181 // Functions set to run during execution of CFG. 182 183 // runCfg executes a node AST by walking its CFG and running node builtin at each step. 184 func runCfg(n *node, f *frame, funcNode, callNode *node) { 185 var exec bltn 186 defer func() { 187 f.mutex.Lock() 188 f.recovered = recover() 189 for _, val := range f.deferred { 190 val[0].Call(val[1:]) 191 } 192 if f.recovered != nil { 193 oNode := originalExecNode(n, exec) 194 if oNode == nil { 195 oNode = n 196 } 197 errorer, ok := f.recovered.(error) 198 // in this specific case, the stdlib would/will suppress the panic, so we 199 // suppress the logging here accordingly, to get a similar and consistent 200 // behavior. 201 if !ok || errorer.Error() != errAbortHandler.Error() { 202 fmt.Fprintln(n.interp.stderr, oNode.cfgErrorf("panic")) 203 } 204 f.mutex.Unlock() 205 panic(f.recovered) 206 } 207 f.mutex.Unlock() 208 }() 209 210 dbg := n.interp.debugger 211 if dbg == nil { 212 for exec := n.exec; exec != nil && f.runid() == n.interp.runid(); { 213 exec = exec(f) 214 } 215 return 216 } 217 218 if n.exec == nil { 219 return 220 } 221 222 dbg.enterCall(funcNode, callNode, f) 223 defer dbg.exitCall(funcNode, callNode, f) 224 225 for m, exec := n, n.exec; f.runid() == n.interp.runid(); { 226 if dbg.exec(m, f) { 227 break 228 } 229 230 exec = exec(f) 231 if exec == nil { 232 break 233 } 234 235 if m == nil { 236 m = originalExecNode(n, exec) 237 continue 238 } 239 240 switch { 241 case isExecNode(m.tnext, exec): 242 m = m.tnext 243 case isExecNode(m.fnext, exec): 244 m = m.fnext 245 default: 246 m = originalExecNode(m, exec) 247 } 248 } 249 } 250 251 func stripReceiverFromArgs(signature string) (string, error) { 252 fields := receiverStripperRxp.FindStringSubmatch(signature) 253 if len(fields) < 5 { 254 return "", errors.New("error while matching method signature") 255 } 256 if fields[3] == ")" { 257 return fmt.Sprintf("func()%s", fields[4]), nil 258 } 259 return fmt.Sprintf("func(%s", fields[4]), nil 260 } 261 262 func typeAssertShort(n *node) { 263 typeAssert(n, true, false) 264 } 265 266 func typeAssertLong(n *node) { 267 typeAssert(n, true, true) 268 } 269 270 func typeAssertStatus(n *node) { 271 typeAssert(n, false, true) 272 } 273 274 func typeAssert(n *node, withResult, withOk bool) { 275 c0, c1 := n.child[0], n.child[1] 276 value := genValue(c0) // input value 277 var value0, value1 func(*frame) reflect.Value 278 setStatus := false 279 switch { 280 case withResult && withOk: 281 value0 = genValue(n.anc.child[0]) // returned result 282 value1 = genValue(n.anc.child[1]) // returned status 283 setStatus = n.anc.child[1].ident != "_" // do not assign status to "_" 284 case withResult && !withOk: 285 value0 = genValue(n) // returned result 286 case !withResult && withOk: 287 value1 = genValue(n.anc.child[1]) // returned status 288 setStatus = n.anc.child[1].ident != "_" // do not assign status to "_" 289 } 290 291 typ := c1.typ // type to assert or convert to 292 typID := typ.id() 293 rtype := typ.refType(nil) // type to assert 294 next := getExec(n.tnext) 295 296 switch { 297 case isInterfaceSrc(typ): 298 n.exec = func(f *frame) bltn { 299 valf := value(f) 300 v, ok := valf.Interface().(valueInterface) 301 if setStatus { 302 defer func() { 303 value1(f).SetBool(ok) 304 }() 305 } 306 if !ok { 307 if !withOk { 308 panic(n.cfgErrorf("interface conversion: nil is not %v", typID)) 309 } 310 return next 311 } 312 if c0.typ.cat == valueT { 313 valf = reflect.ValueOf(v) 314 } 315 if v.node.typ.id() == typID { 316 if withResult { 317 value0(f).Set(valf) 318 } 319 return next 320 } 321 m0 := v.node.typ.methods() 322 m1 := typ.methods() 323 if len(m0) < len(m1) { 324 ok = false 325 if !withOk { 326 panic(n.cfgErrorf("interface conversion: %v is not %v", v.node.typ.id(), typID)) 327 } 328 return next 329 } 330 331 for k, meth1 := range m1 { 332 var meth0 string 333 meth0, ok = m0[k] 334 if !ok { 335 return next 336 } 337 // As far as we know this equality check can fail because they are two ways to 338 // represent the signature of a method: one where the receiver appears before the 339 // func keyword, and one where it is just a func signature, and the receiver is 340 // seen as the first argument. That's why if that equality fails, we try harder to 341 // compare them afterwards. Hopefully that is the only reason this equality can fail. 342 if meth0 == meth1 { 343 continue 344 } 345 tm := lookupFieldOrMethod(v.node.typ, k) 346 if tm == nil { 347 ok = false 348 return next 349 } 350 351 var err error 352 meth0, err = stripReceiverFromArgs(meth0) 353 if err != nil { 354 ok = false 355 return next 356 } 357 358 if meth0 != meth1 { 359 ok = false 360 return next 361 } 362 } 363 364 if withResult { 365 value0(f).Set(valf) 366 } 367 return next 368 } 369 case isInterface(typ): 370 n.exec = func(f *frame) bltn { 371 var leftType reflect.Type 372 v := value(f) 373 val, ok := v.Interface().(valueInterface) 374 if setStatus { 375 defer func() { 376 value1(f).SetBool(ok) 377 }() 378 } 379 if ok && val.node.typ.cat != valueT { 380 m0 := val.node.typ.methods() 381 m1 := typ.methods() 382 if len(m0) < len(m1) { 383 ok = false 384 return next 385 } 386 387 for k, meth1 := range m1 { 388 var meth0 string 389 meth0, ok = m0[k] 390 if !ok { 391 return next 392 } 393 if meth0 != meth1 { 394 ok = false 395 return next 396 } 397 } 398 399 if withResult { 400 value0(f).Set(genInterfaceWrapper(val.node, rtype)(f)) 401 } 402 ok = true 403 return next 404 } 405 406 if ok { 407 v = val.value 408 leftType = val.node.typ.rtype 409 } else { 410 v = v.Elem() 411 leftType = v.Type() 412 ok = true 413 } 414 ok = v.IsValid() 415 if !ok { 416 if !withOk { 417 panic(n.cfgErrorf("interface conversion: interface {} is nil, not %s", rtype.String())) 418 } 419 return next 420 } 421 ok = canAssertTypes(leftType, rtype) 422 if !ok { 423 if !withOk { 424 method := firstMissingMethod(leftType, rtype) 425 panic(n.cfgErrorf("interface conversion: %s is not %s: missing method %s", leftType.String(), rtype.String(), method)) 426 } 427 return next 428 } 429 if withResult { 430 value0(f).Set(v) 431 } 432 return next 433 } 434 case isEmptyInterface(n.child[0].typ): 435 n.exec = func(f *frame) bltn { 436 var ok bool 437 if setStatus { 438 defer func() { 439 value1(f).SetBool(ok) 440 }() 441 } 442 val := value(f) 443 concrete := val.Interface() 444 ctyp := reflect.TypeOf(concrete) 445 446 if vv, ok := concrete.(valueInterface); ok { 447 ctyp = vv.value.Type() 448 concrete = vv.value.Interface() 449 } 450 ok = canAssertTypes(ctyp, rtype) 451 if !ok { 452 if !withOk { 453 // TODO(mpl): think about whether this should ever happen. 454 if ctyp == nil { 455 panic(n.cfgErrorf("interface conversion: interface {} is nil, not %s", rtype.String())) 456 } 457 panic(n.cfgErrorf("interface conversion: interface {} is %s, not %s", ctyp.String(), rtype.String())) 458 } 459 return next 460 } 461 if withResult { 462 if isInterfaceSrc(typ) { 463 // TODO(mpl): this requires more work. the wrapped node is not complete enough. 464 value0(f).Set(reflect.ValueOf(valueInterface{n.child[0], reflect.ValueOf(concrete)})) 465 } else { 466 value0(f).Set(reflect.ValueOf(concrete)) 467 } 468 } 469 return next 470 } 471 case n.child[0].typ.cat == valueT || n.child[0].typ.cat == errorT: 472 n.exec = func(f *frame) bltn { 473 v := value(f).Elem() 474 ok := v.IsValid() 475 if setStatus { 476 defer func() { 477 value1(f).SetBool(ok) 478 }() 479 } 480 if !ok { 481 if !withOk { 482 panic(n.cfgErrorf("interface conversion: interface {} is nil, not %s", rtype.String())) 483 } 484 return next 485 } 486 v = valueInterfaceValue(v) 487 if vt := v.Type(); vt.Kind() == reflect.Struct && vt.Field(0).Name == "IValue" { 488 // Value is retrieved from an interface wrapper. 489 v = v.Field(0).Elem() 490 } 491 ok = canAssertTypes(v.Type(), rtype) 492 if !ok { 493 if !withOk { 494 method := firstMissingMethod(v.Type(), rtype) 495 panic(n.cfgErrorf("interface conversion: %s is not %s: missing method %s", v.Type().String(), rtype.String(), method)) 496 } 497 return next 498 } 499 if withResult { 500 value0(f).Set(v) 501 } 502 return next 503 } 504 default: 505 n.exec = func(f *frame) bltn { 506 v, ok := value(f).Interface().(valueInterface) 507 if setStatus { 508 defer func() { 509 value1(f).SetBool(ok) 510 }() 511 } 512 if !ok || !v.value.IsValid() { 513 ok = false 514 if !withOk { 515 panic(n.cfgErrorf("interface conversion: interface {} is nil, not %s", rtype.String())) 516 } 517 return next 518 } 519 520 ok = canAssertTypes(v.value.Type(), rtype) 521 if !ok { 522 if !withOk { 523 panic(n.cfgErrorf("interface conversion: interface {} is %s, not %s", v.value.Type().String(), rtype.String())) 524 } 525 return next 526 } 527 if withResult { 528 value0(f).Set(v.value) 529 } 530 return next 531 } 532 } 533 } 534 535 func canAssertTypes(src, dest reflect.Type) bool { 536 if dest == nil { 537 return false 538 } 539 if src == dest { 540 return true 541 } 542 if dest.Kind() == reflect.Interface && src.Implements(dest) { 543 return true 544 } 545 if src == nil { 546 return false 547 } 548 if src.AssignableTo(dest) { 549 return true 550 } 551 return false 552 } 553 554 func firstMissingMethod(src, dest reflect.Type) string { 555 for i := 0; i < dest.NumMethod(); i++ { 556 m := dest.Method(i).Name 557 if _, ok := src.MethodByName(m); !ok { 558 return m 559 } 560 } 561 return "" 562 } 563 564 func convert(n *node) { 565 dest := genValue(n) 566 c := n.child[1] 567 typ := n.child[0].typ.frameType() 568 next := getExec(n.tnext) 569 570 if c.isNil() { // convert nil to type 571 // TODO(mpl): Try to completely remove, as maybe frameType already does the job for interfaces. 572 if isInterfaceSrc(n.child[0].typ) && !isEmptyInterface(n.child[0].typ) { 573 typ = valueInterfaceType 574 } 575 n.exec = func(f *frame) bltn { 576 dest(f).Set(reflect.New(typ).Elem()) 577 return next 578 } 579 return 580 } 581 582 doConvert := true 583 var value func(*frame) reflect.Value 584 switch { 585 case isFuncSrc(c.typ): 586 value = genFunctionWrapper(c) 587 default: 588 value = genValue(c) 589 } 590 591 for _, con := range n.interp.hooks.convert { 592 if c.typ.rtype == nil { 593 continue 594 } 595 596 fn := con(c.typ.rtype, typ) 597 if fn == nil { 598 continue 599 } 600 n.exec = func(f *frame) bltn { 601 fn(value(f), dest(f)) 602 return next 603 } 604 return 605 } 606 607 n.exec = func(f *frame) bltn { 608 if doConvert { 609 dest(f).Set(value(f).Convert(typ)) 610 } else { 611 dest(f).Set(value(f)) 612 } 613 return next 614 } 615 } 616 617 // assignFromCall assigns values from a function call. 618 func assignFromCall(n *node) { 619 ncall := n.lastChild() 620 l := len(n.child) - 1 621 if n.anc.kind == varDecl && n.child[l-1].isType(n.scope) { 622 // Ignore the type in the assignment if it is part of a variable declaration. 623 l-- 624 } 625 dvalue := make([]func(*frame) reflect.Value, l) 626 for i := range dvalue { 627 if n.child[i].ident == "_" { 628 continue 629 } 630 dvalue[i] = genValue(n.child[i]) 631 } 632 next := getExec(n.tnext) 633 n.exec = func(f *frame) bltn { 634 for i, v := range dvalue { 635 if v == nil { 636 continue 637 } 638 s := f.data[ncall.findex+i] 639 c := n.child[i] 640 if n.kind == defineXStmt && !c.redeclared { 641 // Recreate destination value in case of define statement, 642 // to preserve previous value possibly in use by a closure. 643 data := getFrame(f, c.level).data 644 data[c.findex] = reflect.New(data[c.findex].Type()).Elem() 645 data[c.findex].Set(s) 646 continue 647 } 648 v(f).Set(s) 649 } 650 return next 651 } 652 } 653 654 func assign(n *node) { 655 next := getExec(n.tnext) 656 dvalue := make([]func(*frame) reflect.Value, n.nleft) 657 ivalue := make([]func(*frame) reflect.Value, n.nleft) 658 svalue := make([]func(*frame) reflect.Value, n.nleft) 659 var sbase int 660 if n.nright > 0 { 661 sbase = len(n.child) - n.nright 662 } 663 664 for i := 0; i < n.nleft; i++ { 665 dest, src := n.child[i], n.child[sbase+i] 666 if isNamedFuncSrc(src.typ) { 667 svalue[i] = genFuncValue(src) 668 } else { 669 svalue[i] = genDestValue(dest.typ, src) 670 } 671 if isMapEntry(dest) { 672 if isInterfaceSrc(dest.child[1].typ) { // key 673 ivalue[i] = genValueInterface(dest.child[1]) 674 } else { 675 ivalue[i] = genValue(dest.child[1]) 676 } 677 dvalue[i] = genValue(dest.child[0]) 678 } else { 679 dvalue[i] = genValue(dest) 680 } 681 } 682 683 if n.nleft == 1 { 684 // Single assign operation. 685 switch s, d, i := svalue[0], dvalue[0], ivalue[0]; { 686 case n.child[0].ident == "_": 687 n.exec = func(f *frame) bltn { 688 return next 689 } 690 case i != nil: 691 n.exec = func(f *frame) bltn { 692 d(f).SetMapIndex(i(f), s(f)) 693 return next 694 } 695 case n.kind == defineStmt: 696 l := n.level 697 ind := n.findex 698 n.exec = func(f *frame) bltn { 699 data := getFrame(f, l).data 700 data[ind] = reflect.New(data[ind].Type()).Elem() 701 data[ind].Set(s(f)) 702 return next 703 } 704 default: 705 n.exec = func(f *frame) bltn { 706 d(f).Set(s(f)) 707 return next 708 } 709 } 710 return 711 } 712 713 // Multi assign operation. 714 types := make([]reflect.Type, n.nright) 715 index := make([]int, n.nright) 716 level := make([]int, n.nright) 717 718 for i := range types { 719 var t reflect.Type 720 switch typ := n.child[sbase+i].typ; { 721 case isInterfaceSrc(typ): 722 t = valueInterfaceType 723 default: 724 t = typ.TypeOf() 725 } 726 types[i] = t 727 index[i] = n.child[i].findex 728 level[i] = n.child[i].level 729 } 730 731 if n.kind == defineStmt { 732 // Handle a multiple var declararation / assign. It cannot be a swap. 733 n.exec = func(f *frame) bltn { 734 for i, s := range svalue { 735 if n.child[i].ident == "_" { 736 continue 737 } 738 data := getFrame(f, level[i]).data 739 j := index[i] 740 data[j] = reflect.New(data[j].Type()).Elem() 741 data[j].Set(s(f)) 742 } 743 return next 744 } 745 return 746 } 747 748 // To handle possible swap in multi-assign: 749 // evaluate and copy all values in assign right hand side into temporary 750 // then evaluate assign left hand side and copy temporary into it 751 n.exec = func(f *frame) bltn { 752 t := make([]reflect.Value, len(svalue)) 753 for i, s := range svalue { 754 if n.child[i].ident == "_" { 755 continue 756 } 757 t[i] = reflect.New(types[i]).Elem() 758 t[i].Set(s(f)) 759 } 760 for i, d := range dvalue { 761 if n.child[i].ident == "_" { 762 continue 763 } 764 if j := ivalue[i]; j != nil { 765 d(f).SetMapIndex(j(f), t[i]) // Assign a map entry 766 } else { 767 d(f).Set(t[i]) // Assign a var or array/slice entry 768 } 769 } 770 return next 771 } 772 } 773 774 func not(n *node) { 775 dest := genValue(n) 776 value := genValue(n.child[0]) 777 tnext := getExec(n.tnext) 778 779 if n.fnext != nil { 780 fnext := getExec(n.fnext) 781 n.exec = func(f *frame) bltn { 782 if !value(f).Bool() { 783 dest(f).SetBool(true) 784 return tnext 785 } 786 dest(f).SetBool(false) 787 return fnext 788 } 789 } else { 790 n.exec = func(f *frame) bltn { 791 dest(f).SetBool(!value(f).Bool()) 792 return tnext 793 } 794 } 795 } 796 797 func addr(n *node) { 798 dest := genValue(n) 799 next := getExec(n.tnext) 800 c0 := n.child[0] 801 value := genValue(c0) 802 803 if isInterfaceSrc(c0.typ) || isPtrSrc(c0.typ) { 804 i := n.findex 805 l := n.level 806 n.exec = func(f *frame) bltn { 807 getFrame(f, l).data[i] = value(f).Addr() 808 return next 809 } 810 return 811 } 812 813 n.exec = func(f *frame) bltn { 814 dest(f).Set(value(f).Addr()) 815 return next 816 } 817 } 818 819 func deref(n *node) { 820 value := genValue(n.child[0]) 821 tnext := getExec(n.tnext) 822 i := n.findex 823 l := n.level 824 825 if n.fnext != nil { 826 fnext := getExec(n.fnext) 827 n.exec = func(f *frame) bltn { 828 r := value(f).Elem() 829 if r.Bool() { 830 getFrame(f, l).data[i] = r 831 return tnext 832 } 833 return fnext 834 } 835 } else { 836 n.exec = func(f *frame) bltn { 837 getFrame(f, l).data[i] = value(f).Elem() 838 return tnext 839 } 840 } 841 } 842 843 func _print(n *node) { 844 child := n.child[1:] 845 values := make([]func(*frame) reflect.Value, len(child)) 846 for i, c := range child { 847 values[i] = genValue(c) 848 } 849 out := n.interp.stdout 850 851 genBuiltinDeferWrapper(n, values, nil, func(args []reflect.Value) []reflect.Value { 852 for i, value := range args { 853 if i > 0 { 854 fmt.Fprintf(out, " ") 855 } 856 fmt.Fprintf(out, "%v", value) 857 } 858 return nil 859 }) 860 } 861 862 func _println(n *node) { 863 child := n.child[1:] 864 values := make([]func(*frame) reflect.Value, len(child)) 865 for i, c := range child { 866 values[i] = genValue(c) 867 } 868 out := n.interp.stdout 869 870 genBuiltinDeferWrapper(n, values, nil, func(args []reflect.Value) []reflect.Value { 871 for i, value := range args { 872 if i > 0 { 873 fmt.Fprintf(out, " ") 874 } 875 fmt.Fprintf(out, "%v", value) 876 } 877 fmt.Fprintln(out, "") 878 return nil 879 }) 880 } 881 882 func _recover(n *node) { 883 tnext := getExec(n.tnext) 884 dest := genValue(n) 885 886 n.exec = func(f *frame) bltn { 887 if f.anc.recovered == nil { 888 // TODO(mpl): maybe we don't need that special case, and we're just forgetting to unwrap the valueInterface somewhere else. 889 if isEmptyInterface(n.typ) { 890 return tnext 891 } 892 dest(f).Set(reflect.ValueOf(valueInterface{})) 893 return tnext 894 } 895 896 if isEmptyInterface(n.typ) { 897 dest(f).Set(reflect.ValueOf(f.anc.recovered)) 898 } else { 899 dest(f).Set(reflect.ValueOf(valueInterface{n, reflect.ValueOf(f.anc.recovered)})) 900 } 901 f.anc.recovered = nil 902 return tnext 903 } 904 } 905 906 func _panic(n *node) { 907 value := genValue(n.child[1]) 908 909 n.exec = func(f *frame) bltn { 910 panic(value(f)) 911 } 912 } 913 914 func genBuiltinDeferWrapper(n *node, in, out []func(*frame) reflect.Value, fn func([]reflect.Value) []reflect.Value) { 915 next := getExec(n.tnext) 916 917 if n.anc.kind == deferStmt { 918 n.exec = func(f *frame) bltn { 919 val := make([]reflect.Value, len(in)+1) 920 inTypes := make([]reflect.Type, len(in)) 921 for i, v := range in { 922 val[i+1] = v(f) 923 inTypes[i] = val[i+1].Type() 924 } 925 outTypes := make([]reflect.Type, len(out)) 926 for i, v := range out { 927 outTypes[i] = v(f).Type() 928 } 929 930 funcType := reflect.FuncOf(inTypes, outTypes, false) 931 val[0] = reflect.MakeFunc(funcType, fn) 932 f.deferred = append([][]reflect.Value{val}, f.deferred...) 933 return next 934 } 935 return 936 } 937 938 n.exec = func(f *frame) bltn { 939 val := make([]reflect.Value, len(in)) 940 for i, v := range in { 941 val[i] = v(f) 942 } 943 944 dests := fn(val) 945 946 for i, dest := range dests { 947 out[i](f).Set(dest) 948 } 949 return next 950 } 951 } 952 953 func genFunctionWrapper(n *node) func(*frame) reflect.Value { 954 var def *node 955 var ok bool 956 957 if def, ok = n.val.(*node); !ok { 958 return genValueAsFunctionWrapper(n) 959 } 960 start := def.child[3].start 961 numRet := len(def.typ.ret) 962 var rcvr func(*frame) reflect.Value 963 964 if n.recv != nil { 965 rcvr = genValueRecv(n) 966 } 967 funcType := n.typ.TypeOf() 968 969 return func(f *frame) reflect.Value { 970 if n.frame != nil { // Use closure context if defined. 971 f = n.frame 972 } 973 return reflect.MakeFunc(funcType, func(in []reflect.Value) []reflect.Value { 974 // Allocate and init local frame. All values to be settable and addressable. 975 fr := newFrame(f, len(def.types), f.runid()) 976 d := fr.data 977 for i, t := range def.types { 978 d[i] = reflect.New(t).Elem() 979 } 980 981 if rcvr == nil { 982 d = d[numRet:] 983 } else { 984 // Copy method receiver as first argument. 985 src, dest := rcvr(f), d[numRet] 986 sk, dk := src.Kind(), dest.Kind() 987 for { 988 vs, ok := src.Interface().(valueInterface) 989 if !ok { 990 break 991 } 992 src = vs.value 993 sk = src.Kind() 994 } 995 switch { 996 case sk == reflect.Ptr && dk != reflect.Ptr: 997 dest.Set(src.Elem()) 998 case sk != reflect.Ptr && dk == reflect.Ptr: 999 dest.Set(src.Addr()) 1000 default: 1001 dest.Set(src) 1002 } 1003 d = d[numRet+1:] 1004 } 1005 1006 // Copy function input arguments in local frame. 1007 for i, arg := range in { 1008 if i >= len(d) { 1009 // In case of unused arg, there may be not even a frame entry allocated, just skip. 1010 break 1011 } 1012 typ := def.typ.arg[i] 1013 switch { 1014 case isEmptyInterface(typ) || typ.TypeOf() == valueInterfaceType: 1015 d[i].Set(arg) 1016 case isInterfaceSrc(typ): 1017 d[i].Set(reflect.ValueOf(valueInterface{value: arg.Elem()})) 1018 default: 1019 d[i].Set(arg) 1020 } 1021 } 1022 1023 // Interpreter code execution. 1024 runCfg(start, fr, def, n) 1025 1026 return fr.data[:numRet] 1027 }) 1028 } 1029 } 1030 1031 func genInterfaceWrapper(n *node, typ reflect.Type) func(*frame) reflect.Value { 1032 value := genValue(n) 1033 if typ == nil || typ.Kind() != reflect.Interface || typ.NumMethod() == 0 || n.typ.cat == valueT { 1034 return value 1035 } 1036 tc := n.typ.cat 1037 if tc != structT { 1038 // Always force wrapper generation for struct types, as they may contain 1039 // embedded interface fields which require wrapping, even if reported as 1040 // implementing typ by reflect. 1041 if nt := n.typ.frameType(); nt != nil && nt.Implements(typ) { 1042 return value 1043 } 1044 } 1045 1046 // Retrieve methods from the interface wrapper, which is a struct where all fields 1047 // except the first define the methods to implement. 1048 // As the field name was generated with a prefixed first character (in order to avoid 1049 // collisions with method names), this first character is ignored in comparisons. 1050 wrap := getWrapper(n, typ) 1051 mn := wrap.NumField() - 1 1052 names := make([]string, mn) 1053 methods := make([]*node, mn) 1054 indexes := make([][]int, mn) 1055 for i := 0; i < mn; i++ { 1056 names[i] = wrap.Field(i + 1).Name[1:] 1057 methods[i], indexes[i] = n.typ.lookupMethod(names[i]) 1058 if methods[i] == nil && n.typ.cat != nilT { 1059 // interpreted method not found, look for binary method, possibly embedded 1060 _, indexes[i], _, _ = n.typ.lookupBinMethod(names[i]) 1061 } 1062 } 1063 1064 return func(f *frame) reflect.Value { 1065 v := value(f) 1066 if tc != structT && v.Type().Implements(typ) { 1067 return v 1068 } 1069 switch v.Kind() { 1070 case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: 1071 if v.IsNil() { 1072 return reflect.New(typ).Elem() 1073 } 1074 } 1075 var n2 *node 1076 if vi, ok := v.Interface().(valueInterface); ok { 1077 n2 = vi.node 1078 } 1079 v = getConcreteValue(v) 1080 w := reflect.New(wrap).Elem() 1081 w.Field(0).Set(v) 1082 for i, m := range methods { 1083 if m == nil { 1084 // First direct method lookup on field. 1085 if r := methodByName(v, names[i], indexes[i]); r.IsValid() { 1086 w.Field(i + 1).Set(r) 1087 continue 1088 } 1089 if n2 == nil { 1090 panic(n.cfgErrorf("method not found: %s", names[i])) 1091 } 1092 // Method lookup in embedded valueInterface. 1093 m2, i2 := n2.typ.lookupMethod(names[i]) 1094 if m2 != nil { 1095 nod := *m2 1096 nod.recv = &receiver{n, v, i2} 1097 w.Field(i + 1).Set(genFunctionWrapper(&nod)(f)) 1098 continue 1099 } 1100 panic(n.cfgErrorf("method not found: %s", names[i])) 1101 } 1102 nod := *m 1103 nod.recv = &receiver{n, v, indexes[i]} 1104 w.Field(i + 1).Set(genFunctionWrapper(&nod)(f)) 1105 } 1106 return w 1107 } 1108 } 1109 1110 // methodByName returns the method corresponding to name on value, or nil if not found. 1111 // The search is extended on valueInterface wrapper if present. 1112 // If valid, the returned value is a method function with the receiver already set 1113 // (no need to pass it at call). 1114 func methodByName(value reflect.Value, name string, index []int) (v reflect.Value) { 1115 if vi, ok := value.Interface().(valueInterface); ok { 1116 if v = getConcreteValue(vi.value).MethodByName(name); v.IsValid() { 1117 return 1118 } 1119 } 1120 if v = value.MethodByName(name); v.IsValid() { 1121 return 1122 } 1123 for value.Kind() == reflect.Ptr { 1124 value = value.Elem() 1125 if checkFieldIndex(value.Type(), index) { 1126 value = value.FieldByIndex(index) 1127 } 1128 if v = value.MethodByName(name); v.IsValid() { 1129 return 1130 } 1131 } 1132 return 1133 } 1134 1135 func checkFieldIndex(typ reflect.Type, index []int) bool { 1136 if len(index) == 0 { 1137 return false 1138 } 1139 t := typ 1140 for t.Kind() == reflect.Ptr { 1141 t = t.Elem() 1142 } 1143 if t.Kind() != reflect.Struct { 1144 return false 1145 } 1146 i := index[0] 1147 if i >= t.NumField() { 1148 return false 1149 } 1150 if len(index) > 1 { 1151 return checkFieldIndex(t.Field(i).Type, index[1:]) 1152 } 1153 return true 1154 } 1155 1156 func call(n *node) { 1157 goroutine := n.anc.kind == goStmt 1158 c0 := n.child[0] 1159 value := genValue(c0) 1160 var values []func(*frame) reflect.Value 1161 1162 numRet := len(c0.typ.ret) 1163 variadic := variadicPos(n) 1164 child := n.child[1:] 1165 tnext := getExec(n.tnext) 1166 fnext := getExec(n.fnext) 1167 hasVariadicArgs := n.action == aCallSlice // callSlice implies variadic call with ellipsis. 1168 1169 // Compute input argument value functions. 1170 for i, c := range child { 1171 var arg *itype 1172 if variadic >= 0 && i >= variadic { 1173 arg = c0.typ.arg[variadic].val 1174 } else { 1175 arg = c0.typ.arg[i] 1176 } 1177 switch { 1178 case isBinCall(c, c.scope): 1179 // Handle nested function calls: pass returned values as arguments. 1180 numOut := c.child[0].typ.rtype.NumOut() 1181 for j := 0; j < numOut; j++ { 1182 ind := c.findex + j 1183 if hasVariadicArgs || !isInterfaceSrc(arg) || isEmptyInterface(arg) { 1184 values = append(values, func(f *frame) reflect.Value { return f.data[ind] }) 1185 continue 1186 } 1187 values = append(values, func(f *frame) reflect.Value { 1188 return reflect.ValueOf(valueInterface{value: f.data[ind]}) 1189 }) 1190 } 1191 case isRegularCall(c): 1192 // Arguments are return values of a nested function call. 1193 cc0 := c.child[0] 1194 for j := range cc0.typ.ret { 1195 ind := c.findex + j 1196 if hasVariadicArgs || !isInterfaceSrc(arg) || isEmptyInterface(arg) { 1197 values = append(values, func(f *frame) reflect.Value { return f.data[ind] }) 1198 continue 1199 } 1200 values = append(values, func(f *frame) reflect.Value { 1201 return reflect.ValueOf(valueInterface{node: cc0.typ.ret[j].node, value: f.data[ind]}) 1202 }) 1203 } 1204 default: 1205 if c.kind == basicLit || c.rval.IsValid() { 1206 argType := arg.TypeOf() 1207 convertLiteralValue(c, argType) 1208 } 1209 switch { 1210 case hasVariadicArgs: 1211 values = append(values, genValue(c)) 1212 case isInterfaceSrc(arg) && (!isEmptyInterface(arg) || len(c.typ.method) > 0): 1213 values = append(values, genValueInterface(c)) 1214 case isInterfaceBin(arg): 1215 values = append(values, genInterfaceWrapper(c, arg.rtype)) 1216 case isFuncSrc(arg): 1217 values = append(values, genFuncValue(c)) 1218 default: 1219 values = append(values, genValue(c)) 1220 } 1221 } 1222 } 1223 1224 // Compute output argument value functions. 1225 rtypes := c0.typ.ret 1226 rvalues := make([]func(*frame) reflect.Value, len(rtypes)) 1227 switch n.anc.kind { 1228 case defineXStmt, assignXStmt: 1229 l := n.level 1230 for i := range rvalues { 1231 c := n.anc.child[i] 1232 switch { 1233 case c.ident == "_": 1234 // Skip assigning return value to blank var. 1235 case isInterfaceSrc(c.typ) && !isEmptyInterface(c.typ) && !isInterfaceSrc(rtypes[i]): 1236 rvalues[i] = genValueInterfaceValue(c) 1237 default: 1238 j := n.findex + i 1239 rvalues[i] = func(f *frame) reflect.Value { return getFrame(f, l).data[j] } 1240 } 1241 } 1242 case returnStmt: 1243 // Function call from a return statement: forward return values (always at frame start). 1244 for i := range rtypes { 1245 j := n.findex + i 1246 // Set the return value location in return value of caller frame. 1247 rvalues[i] = func(f *frame) reflect.Value { return f.data[j] } 1248 } 1249 default: 1250 // Multiple return values frame index are indexed from the node frame index. 1251 l := n.level 1252 for i := range rtypes { 1253 j := n.findex + i 1254 rvalues[i] = func(f *frame) reflect.Value { return getFrame(f, l).data[j] } 1255 } 1256 } 1257 1258 if n.anc.kind == deferStmt { 1259 // Store function call in frame for deferred execution. 1260 value = genFunctionWrapper(c0) 1261 n.exec = func(f *frame) bltn { 1262 val := make([]reflect.Value, len(values)+1) 1263 val[0] = value(f) 1264 for i, v := range values { 1265 val[i+1] = v(f) 1266 } 1267 f.deferred = append([][]reflect.Value{val}, f.deferred...) 1268 return tnext 1269 } 1270 return 1271 } 1272 1273 n.exec = func(f *frame) bltn { 1274 var def *node 1275 var ok bool 1276 1277 bf := value(f) 1278 1279 if def, ok = bf.Interface().(*node); ok { 1280 bf = def.rval 1281 } 1282 1283 // Call bin func if defined 1284 if bf.IsValid() { 1285 var callf func([]reflect.Value) []reflect.Value 1286 1287 // Lambda definitions are necessary here. Due to reflect internals, 1288 // having `callf = bf.Call` or `callf = bf.CallSlice` does not work. 1289 //nolint:gocritic 1290 if hasVariadicArgs { 1291 callf = func(in []reflect.Value) []reflect.Value { return bf.CallSlice(in) } 1292 } else { 1293 callf = func(in []reflect.Value) []reflect.Value { return bf.Call(in) } 1294 } 1295 1296 if goroutine { 1297 // Goroutine's arguments should be copied. 1298 in := make([]reflect.Value, len(values)) 1299 for i, v := range values { 1300 value := v(f) 1301 in[i] = reflect.New(value.Type()).Elem() 1302 in[i].Set(value) 1303 } 1304 1305 go callf(in) 1306 return tnext 1307 } 1308 1309 in := make([]reflect.Value, len(values)) 1310 for i, v := range values { 1311 in[i] = v(f) 1312 } 1313 out := callf(in) 1314 for i, v := range rvalues { 1315 if v != nil { 1316 v(f).Set(out[i]) 1317 } 1318 } 1319 if fnext != nil && !out[0].Bool() { 1320 return fnext 1321 } 1322 return tnext 1323 } 1324 1325 anc := f 1326 // Get closure frame context (if any) 1327 if def.frame != nil { 1328 anc = def.frame 1329 } 1330 nf := newFrame(anc, len(def.types), anc.runid()) 1331 var vararg reflect.Value 1332 1333 // Init return values 1334 for i, v := range rvalues { 1335 if v != nil { 1336 nf.data[i] = v(f) 1337 } else { 1338 nf.data[i] = reflect.New(def.types[i]).Elem() 1339 } 1340 } 1341 1342 // Init local frame values 1343 for i, t := range def.types[numRet:] { 1344 nf.data[numRet+i] = reflect.New(t).Elem() 1345 } 1346 1347 // Init variadic argument vector 1348 if variadic >= 0 { 1349 vararg = nf.data[numRet+variadic] 1350 } 1351 1352 // Copy input parameters from caller 1353 if dest := nf.data[numRet:]; len(dest) > 0 { 1354 for i, v := range values { 1355 switch { 1356 case variadic >= 0 && i >= variadic: 1357 if v(f).Type() == vararg.Type() { 1358 vararg.Set(v(f)) 1359 } else { 1360 vararg.Set(reflect.Append(vararg, v(f))) 1361 } 1362 default: 1363 val := v(f) 1364 if val.IsZero() && dest[i].Kind() != reflect.Interface { 1365 // Work around a recursive struct zero interface issue. 1366 // Once there is a better way to handle this case, the dest can just be set. 1367 continue 1368 } 1369 if nod, ok := val.Interface().(*node); ok && nod.recv != nil { 1370 // An interpreted method is passed as value in a function call. 1371 // It must be wrapped now, otherwise the receiver will be missing 1372 // at the method call (#1332). 1373 // TODO (marc): wrapping interpreted functions should be always done 1374 // everywhere at runtime to simplify the whole code, 1375 // but it requires deeper refactoring. 1376 dest[i] = genFunctionWrapper(nod)(f) 1377 continue 1378 } 1379 dest[i].Set(val) 1380 } 1381 } 1382 } 1383 1384 // Execute function body 1385 if goroutine { 1386 go runCfg(def.child[3].start, nf, def, n) 1387 return tnext 1388 } 1389 runCfg(def.child[3].start, nf, def, n) 1390 1391 // Handle branching according to boolean result 1392 if fnext != nil && !nf.data[0].Bool() { 1393 return fnext 1394 } 1395 return tnext 1396 } 1397 } 1398 1399 func getFrame(f *frame, l int) *frame { 1400 switch l { 1401 case globalFrame: 1402 return f.root 1403 case 0: 1404 return f 1405 case 1: 1406 return f.anc 1407 case 2: 1408 return f.anc.anc 1409 } 1410 for ; l > 0; l-- { 1411 f = f.anc 1412 } 1413 return f 1414 } 1415 1416 // Callbin calls a function from a bin import, accessible through reflect. 1417 func callBin(n *node) { 1418 tnext := getExec(n.tnext) 1419 fnext := getExec(n.fnext) 1420 child := n.child[1:] 1421 c0 := n.child[0] 1422 value := genValue(c0) 1423 var values []func(*frame) reflect.Value 1424 funcType := c0.typ.rtype 1425 wt := wrappedType(c0) 1426 variadic := -1 1427 if funcType.IsVariadic() { 1428 variadic = funcType.NumIn() - 1 1429 } 1430 // A method signature obtained from reflect.Type includes receiver as 1st arg, except for interface types. 1431 rcvrOffset := 0 1432 if recv := c0.recv; recv != nil && !isInterface(recv.node.typ) { 1433 if variadic > 0 || funcType.NumIn() > len(child) { 1434 rcvrOffset = 1 1435 } 1436 } 1437 1438 // getMapType returns a reflect type suitable for interface wrapper for functions 1439 // with some special processing in case of interface{} argument, i.e. fmt.Printf. 1440 var getMapType func(*itype) reflect.Type 1441 if lr, ok := n.interp.mapTypes[c0.rval]; ok { 1442 getMapType = func(typ *itype) reflect.Type { 1443 for _, rt := range lr { 1444 if typ.implements(&itype{cat: valueT, rtype: rt}) { 1445 return rt 1446 } 1447 } 1448 return nil 1449 } 1450 } 1451 1452 // Determine if we should use `Call` or `CallSlice` on the function Value. 1453 callFn := func(v reflect.Value, in []reflect.Value) []reflect.Value { return v.Call(in) } 1454 if n.action == aCallSlice { 1455 callFn = func(v reflect.Value, in []reflect.Value) []reflect.Value { return v.CallSlice(in) } 1456 } 1457 1458 for i, c := range child { 1459 switch { 1460 case isBinCall(c, c.scope): 1461 // Handle nested function calls: pass returned values as arguments 1462 numOut := c.child[0].typ.rtype.NumOut() 1463 for j := 0; j < numOut; j++ { 1464 ind := c.findex + j 1465 values = append(values, func(f *frame) reflect.Value { return valueInterfaceValue(f.data[ind]) }) 1466 } 1467 case isRegularCall(c): 1468 // Handle nested function calls: pass returned values as arguments 1469 for j := range c.child[0].typ.ret { 1470 ind := c.findex + j 1471 values = append(values, func(f *frame) reflect.Value { return valueInterfaceValue(f.data[ind]) }) 1472 } 1473 default: 1474 if c.kind == basicLit || c.rval.IsValid() { 1475 // Convert literal value (untyped) to function argument type (if not an interface{}) 1476 var argType reflect.Type 1477 if variadic >= 0 && i+rcvrOffset >= variadic { 1478 argType = funcType.In(variadic).Elem() 1479 } else { 1480 argType = funcType.In(i + rcvrOffset) 1481 } 1482 convertLiteralValue(c, argType) 1483 if !reflect.ValueOf(c.val).IsValid() { // Handle "nil" 1484 c.val = reflect.Zero(argType) 1485 } 1486 } 1487 1488 if wt != nil && isInterfaceSrc(wt.arg[i]) { 1489 values = append(values, genValueInterface(c)) 1490 break 1491 } 1492 1493 // defType is the target type for a potential interface wrapper. 1494 var defType reflect.Type 1495 if variadic >= 0 && i+rcvrOffset >= variadic { 1496 defType = funcType.In(variadic) 1497 } else { 1498 defType = funcType.In(rcvrOffset + i) 1499 } 1500 if getMapType != nil { 1501 if rt := getMapType(c.typ); rt != nil { 1502 defType = rt 1503 } 1504 } 1505 1506 switch { 1507 case isEmptyInterface(c.typ): 1508 values = append(values, genValue(c)) 1509 case isInterfaceSrc(c.typ): 1510 values = append(values, genValueInterfaceValue(c)) 1511 case isFuncSrc(c.typ): 1512 values = append(values, genFunctionWrapper(c)) 1513 case c.typ.cat == arrayT || c.typ.cat == variadicT: 1514 if isEmptyInterface(c.typ.val) { 1515 values = append(values, genValueArray(c)) 1516 } else { 1517 values = append(values, genInterfaceWrapper(c, defType)) 1518 } 1519 case isPtrSrc(c.typ): 1520 if c.typ.val.cat == valueT { 1521 values = append(values, genValue(c)) 1522 } else { 1523 values = append(values, genInterfaceWrapper(c, defType)) 1524 } 1525 case c.typ.cat == valueT: 1526 values = append(values, genValue(c)) 1527 default: 1528 values = append(values, genInterfaceWrapper(c, defType)) 1529 } 1530 } 1531 } 1532 l := len(values) 1533 1534 switch { 1535 case n.anc.kind == deferStmt: 1536 // Store function call in frame for deferred execution. 1537 n.exec = func(f *frame) bltn { 1538 val := make([]reflect.Value, l+1) 1539 val[0] = value(f) 1540 for i, v := range values { 1541 val[i+1] = getBinValue(getMapType, v, f) 1542 } 1543 f.deferred = append([][]reflect.Value{val}, f.deferred...) 1544 return tnext 1545 } 1546 case n.anc.kind == goStmt: 1547 // Execute function in a goroutine, discard results. 1548 n.exec = func(f *frame) bltn { 1549 in := make([]reflect.Value, l) 1550 for i, v := range values { 1551 in[i] = getBinValue(getMapType, v, f) 1552 } 1553 go callFn(value(f), in) 1554 return tnext 1555 } 1556 case fnext != nil: 1557 // Handle branching according to boolean result. 1558 index := n.findex 1559 level := n.level 1560 n.exec = func(f *frame) bltn { 1561 in := make([]reflect.Value, l) 1562 for i, v := range values { 1563 in[i] = getBinValue(getMapType, v, f) 1564 } 1565 res := callFn(value(f), in) 1566 b := res[0].Bool() 1567 getFrame(f, level).data[index].SetBool(b) 1568 if b { 1569 return tnext 1570 } 1571 return fnext 1572 } 1573 default: 1574 switch n.anc.action { 1575 case aAssignX: 1576 // The function call is part of an assign expression, store results direcly 1577 // to assigned location, to avoid an additional frame copy. 1578 // The optimization of aAssign is handled in assign(), and should not 1579 // be handled here. 1580 rvalues := make([]func(*frame) reflect.Value, funcType.NumOut()) 1581 for i := range rvalues { 1582 c := n.anc.child[i] 1583 if c.ident == "_" { 1584 continue 1585 } 1586 if isInterfaceSrc(c.typ) { 1587 rvalues[i] = genValueInterfaceValue(c) 1588 } else { 1589 rvalues[i] = genValue(c) 1590 } 1591 } 1592 n.exec = func(f *frame) bltn { 1593 in := make([]reflect.Value, l) 1594 for i, v := range values { 1595 in[i] = getBinValue(getMapType, v, f) 1596 } 1597 out := callFn(value(f), in) 1598 for i, v := range rvalues { 1599 if v == nil { 1600 continue // Skip assign "_". 1601 } 1602 c := n.anc.child[i] 1603 if n.anc.kind == defineXStmt && !c.redeclared { 1604 // In case of a define statement, the destination value in the frame 1605 // must be recreated. This is necessary to preserve the previous value 1606 // which may be still used in a separate closure. 1607 data := getFrame(f, c.level).data 1608 data[c.findex] = reflect.New(data[c.findex].Type()).Elem() 1609 data[c.findex].Set(out[i]) 1610 continue 1611 } 1612 v(f).Set(out[i]) 1613 } 1614 return tnext 1615 } 1616 case aReturn: 1617 // The function call is part of a return statement, store output results 1618 // directly in the frame location of outputs of the current function. 1619 b := childPos(n) 1620 n.exec = func(f *frame) bltn { 1621 in := make([]reflect.Value, l) 1622 for i, v := range values { 1623 in[i] = getBinValue(getMapType, v, f) 1624 } 1625 out := callFn(value(f), in) 1626 for i, v := range out { 1627 dest := f.data[b+i] 1628 if _, ok := dest.Interface().(valueInterface); ok { 1629 v = reflect.ValueOf(valueInterface{value: v}) 1630 } 1631 dest.Set(v) 1632 } 1633 return tnext 1634 } 1635 default: 1636 n.exec = func(f *frame) bltn { 1637 in := make([]reflect.Value, l) 1638 for i, v := range values { 1639 in[i] = getBinValue(getMapType, v, f) 1640 } 1641 out := callFn(value(f), in) 1642 for i := 0; i < len(out); i++ { 1643 r := out[i] 1644 if r.Kind() == reflect.Func { 1645 getFrame(f, n.level).data[n.findex+i] = r 1646 continue 1647 } 1648 dest := getFrame(f, n.level).data[n.findex+i] 1649 if _, ok := dest.Interface().(valueInterface); ok { 1650 r = reflect.ValueOf(valueInterface{value: r}) 1651 } 1652 dest.Set(r) 1653 } 1654 return tnext 1655 } 1656 } 1657 } 1658 } 1659 1660 func getIndexBinMethod(n *node) { 1661 // dest := genValue(n) 1662 i := n.findex 1663 l := n.level 1664 m := n.val.(int) 1665 value := genValue(n.child[0]) 1666 next := getExec(n.tnext) 1667 1668 n.exec = func(f *frame) bltn { 1669 // Can not use .Set() because dest type contains the receiver and source not 1670 // dest(f).Set(value(f).Method(m)) 1671 getFrame(f, l).data[i] = value(f).Method(m) 1672 return next 1673 } 1674 } 1675 1676 func getIndexBinElemMethod(n *node) { 1677 i := n.findex 1678 l := n.level 1679 m := n.val.(int) 1680 value := genValue(n.child[0]) 1681 next := getExec(n.tnext) 1682 1683 n.exec = func(f *frame) bltn { 1684 // Can not use .Set() because dest type contains the receiver and source not 1685 getFrame(f, l).data[i] = value(f).Elem().Method(m) 1686 return next 1687 } 1688 } 1689 1690 func getIndexBinPtrMethod(n *node) { 1691 i := n.findex 1692 l := n.level 1693 m := n.val.(int) 1694 value := genValue(n.child[0]) 1695 next := getExec(n.tnext) 1696 1697 n.exec = func(f *frame) bltn { 1698 // Can not use .Set() because dest type contains the receiver and source not 1699 getFrame(f, l).data[i] = value(f).Addr().Method(m) 1700 return next 1701 } 1702 } 1703 1704 // getIndexArray returns array value from index. 1705 func getIndexArray(n *node) { 1706 tnext := getExec(n.tnext) 1707 value0 := genValueArray(n.child[0]) // array 1708 i := n.findex 1709 l := n.level 1710 1711 if n.child[1].rval.IsValid() { // constant array index 1712 ai := int(vInt(n.child[1].rval)) 1713 if n.fnext != nil { 1714 fnext := getExec(n.fnext) 1715 n.exec = func(f *frame) bltn { 1716 r := value0(f).Index(ai) 1717 getFrame(f, l).data[i] = r 1718 if r.Bool() { 1719 return tnext 1720 } 1721 return fnext 1722 } 1723 } else { 1724 n.exec = func(f *frame) bltn { 1725 getFrame(f, l).data[i] = value0(f).Index(ai) 1726 return tnext 1727 } 1728 } 1729 } else { 1730 value1 := genValueInt(n.child[1]) // array index 1731 1732 if n.fnext != nil { 1733 fnext := getExec(n.fnext) 1734 n.exec = func(f *frame) bltn { 1735 _, vi := value1(f) 1736 r := value0(f).Index(int(vi)) 1737 getFrame(f, l).data[i] = r 1738 if r.Bool() { 1739 return tnext 1740 } 1741 return fnext 1742 } 1743 } else { 1744 n.exec = func(f *frame) bltn { 1745 _, vi := value1(f) 1746 getFrame(f, l).data[i] = value0(f).Index(int(vi)) 1747 return tnext 1748 } 1749 } 1750 } 1751 } 1752 1753 // getIndexMap retrieves map value from index. 1754 func getIndexMap(n *node) { 1755 dest := genValue(n) 1756 value0 := genValue(n.child[0]) // map 1757 tnext := getExec(n.tnext) 1758 z := reflect.New(n.child[0].typ.frameType().Elem()).Elem() 1759 1760 if n.child[1].rval.IsValid() { // constant map index 1761 mi := n.child[1].rval 1762 1763 switch { 1764 case n.fnext != nil: 1765 fnext := getExec(n.fnext) 1766 n.exec = func(f *frame) bltn { 1767 if v := value0(f).MapIndex(mi); v.IsValid() && v.Bool() { 1768 dest(f).SetBool(true) 1769 return tnext 1770 } 1771 dest(f).Set(z) 1772 return fnext 1773 } 1774 default: 1775 n.exec = func(f *frame) bltn { 1776 if v := value0(f).MapIndex(mi); v.IsValid() { 1777 dest(f).Set(v) 1778 } else { 1779 dest(f).Set(z) 1780 } 1781 return tnext 1782 } 1783 } 1784 } else { 1785 value1 := genValue(n.child[1]) // map index 1786 1787 switch { 1788 case n.fnext != nil: 1789 fnext := getExec(n.fnext) 1790 n.exec = func(f *frame) bltn { 1791 if v := value0(f).MapIndex(value1(f)); v.IsValid() && v.Bool() { 1792 dest(f).SetBool(true) 1793 return tnext 1794 } 1795 dest(f).Set(z) 1796 return fnext 1797 } 1798 default: 1799 n.exec = func(f *frame) bltn { 1800 if v := value0(f).MapIndex(value1(f)); v.IsValid() { 1801 dest(f).Set(v) 1802 } else { 1803 dest(f).Set(z) 1804 } 1805 return tnext 1806 } 1807 } 1808 } 1809 } 1810 1811 // getIndexMap2 retrieves map value from index and set status. 1812 func getIndexMap2(n *node) { 1813 dest := genValue(n.anc.child[0]) // result 1814 value0 := genValue(n.child[0]) // map 1815 value2 := genValue(n.anc.child[1]) // status 1816 next := getExec(n.tnext) 1817 doValue := n.anc.child[0].ident != "_" 1818 doStatus := n.anc.child[1].ident != "_" 1819 1820 if !doValue && !doStatus { 1821 nop(n) 1822 return 1823 } 1824 if n.child[1].rval.IsValid() { // constant map index 1825 mi := n.child[1].rval 1826 switch { 1827 case !doValue: 1828 n.exec = func(f *frame) bltn { 1829 v := value0(f).MapIndex(mi) 1830 value2(f).SetBool(v.IsValid()) 1831 return next 1832 } 1833 default: 1834 n.exec = func(f *frame) bltn { 1835 v := value0(f).MapIndex(mi) 1836 if v.IsValid() { 1837 dest(f).Set(v) 1838 } 1839 if doStatus { 1840 value2(f).SetBool(v.IsValid()) 1841 } 1842 return next 1843 } 1844 } 1845 } else { 1846 value1 := genValue(n.child[1]) // map index 1847 switch { 1848 case !doValue: 1849 n.exec = func(f *frame) bltn { 1850 v := value0(f).MapIndex(value1(f)) 1851 value2(f).SetBool(v.IsValid()) 1852 return next 1853 } 1854 default: 1855 n.exec = func(f *frame) bltn { 1856 v := value0(f).MapIndex(value1(f)) 1857 if v.IsValid() { 1858 dest(f).Set(v) 1859 } 1860 if doStatus { 1861 value2(f).SetBool(v.IsValid()) 1862 } 1863 return next 1864 } 1865 } 1866 } 1867 } 1868 1869 const fork = true // Duplicate frame in frame.clone(). 1870 1871 // getFunc compiles a closure function generator for anonymous functions. 1872 func getFunc(n *node) { 1873 i := n.findex 1874 l := n.level 1875 next := getExec(n.tnext) 1876 1877 n.exec = func(f *frame) bltn { 1878 fr := f.clone(fork) 1879 nod := *n 1880 nod.val = &nod 1881 nod.frame = fr 1882 def := &nod 1883 numRet := len(def.typ.ret) 1884 1885 fct := reflect.MakeFunc(nod.typ.TypeOf(), func(in []reflect.Value) []reflect.Value { 1886 // Allocate and init local frame. All values to be settable and addressable. 1887 fr2 := newFrame(fr, len(def.types), fr.runid()) 1888 d := fr2.data 1889 for i, t := range def.types { 1890 d[i] = reflect.New(t).Elem() 1891 } 1892 d = d[numRet:] 1893 1894 // Copy function input arguments in local frame. 1895 for i, arg := range in { 1896 if i >= len(d) { 1897 // In case of unused arg, there may be not even a frame entry allocated, just skip. 1898 break 1899 } 1900 typ := def.typ.arg[i] 1901 switch { 1902 case isEmptyInterface(typ) || typ.TypeOf() == valueInterfaceType: 1903 d[i].Set(arg) 1904 case isInterfaceSrc(typ): 1905 d[i].Set(reflect.ValueOf(valueInterface{value: arg.Elem()})) 1906 default: 1907 d[i].Set(arg) 1908 } 1909 } 1910 1911 // Interpreter code execution. 1912 runCfg(def.child[3].start, fr2, def, n) 1913 1914 return fr2.data[:numRet] 1915 }) 1916 1917 getFrame(f, l).data[i] = fct 1918 return next 1919 } 1920 } 1921 1922 func getMethod(n *node) { 1923 i := n.findex 1924 l := n.level 1925 next := getExec(n.tnext) 1926 1927 n.exec = func(f *frame) bltn { 1928 fr := f.clone(!fork) 1929 nod := *(n.val.(*node)) 1930 nod.val = &nod 1931 nod.recv = n.recv 1932 nod.frame = fr 1933 getFrame(f, l).data[i] = genFuncValue(&nod)(f) 1934 return next 1935 } 1936 } 1937 1938 func getMethodByName(n *node) { 1939 next := getExec(n.tnext) 1940 value0 := genValue(n.child[0]) 1941 name := n.child[1].ident 1942 i := n.findex 1943 l := n.level 1944 1945 n.exec = func(f *frame) bltn { 1946 // The interface object must be directly accessible, or embedded in a struct (exported anonymous field). 1947 val0 := value0(f) 1948 val, ok := value0(f).Interface().(valueInterface) 1949 if !ok { 1950 // Search the first embedded valueInterface. 1951 for val0.Kind() == reflect.Ptr { 1952 val0 = val0.Elem() 1953 } 1954 for i := 0; i < val0.NumField(); i++ { 1955 fld := val0.Type().Field(i) 1956 if !fld.Anonymous || !fld.IsExported() { 1957 continue 1958 } 1959 if val, ok = val0.Field(i).Interface().(valueInterface); ok { 1960 break 1961 // TODO: should we keep track of all the the vals that are indeed valueInterface, 1962 // so that later on we can call MethodByName on all of them until one matches? 1963 } 1964 } 1965 if !ok { 1966 panic(n.cfgErrorf("invalid interface value %v", val0)) 1967 } 1968 } 1969 // Traverse nested interface values to get the concrete value. 1970 for { 1971 v, ok := val.value.Interface().(valueInterface) 1972 if !ok { 1973 break 1974 } 1975 val = v 1976 } 1977 1978 if met := val.value.MethodByName(name); met.IsValid() { 1979 getFrame(f, l).data[i] = met 1980 return next 1981 } 1982 1983 typ := val.node.typ 1984 if typ.node == nil && typ.cat == valueT { 1985 // It happens with a var of empty interface type, that has value of concrete type 1986 // from runtime, being asserted to "user-defined" interface. 1987 if _, ok := typ.rtype.MethodByName(name); !ok { 1988 panic(n.cfgErrorf("method not found: %s", name)) 1989 } 1990 return next 1991 } 1992 1993 // Finally search method recursively in embedded valueInterfaces. 1994 r, m, li := lookupMethodValue(val, name) 1995 if r.IsValid() { 1996 getFrame(f, l).data[i] = r 1997 return next 1998 } 1999 if m == nil { 2000 panic(n.cfgErrorf("method not found: %s", name)) 2001 } 2002 2003 fr := f.clone(!fork) 2004 nod := *m 2005 nod.val = &nod 2006 nod.recv = &receiver{nil, val.value, li} 2007 nod.frame = fr 2008 getFrame(f, l).data[i] = genFuncValue(&nod)(f) 2009 return next 2010 } 2011 } 2012 2013 // lookupMethodValue recursively looks within val for the method with the given 2014 // name. If a runtime value is found, it is returned in r, otherwise it is returned 2015 // in m, with li as the list of recursive field indexes. 2016 func lookupMethodValue(val valueInterface, name string) (r reflect.Value, m *node, li []int) { 2017 if r = val.value.MethodByName(name); r.IsValid() { 2018 return 2019 } 2020 if m, li = val.node.typ.lookupMethod(name); m != nil { 2021 return 2022 } 2023 if !isStruct(val.node.typ) { 2024 return 2025 } 2026 v := val.value 2027 for v.Type().Kind() == reflect.Ptr { 2028 v = v.Elem() 2029 } 2030 nf := v.NumField() 2031 for i := 0; i < nf; i++ { 2032 vi, ok := v.Field(i).Interface().(valueInterface) 2033 if !ok { 2034 continue 2035 } 2036 if r, m, li = lookupMethodValue(vi, name); m != nil { 2037 li = append([]int{i}, li...) 2038 return 2039 } 2040 } 2041 return 2042 } 2043 2044 func getIndexSeq(n *node) { 2045 value := genValue(n.child[0]) 2046 index := n.val.([]int) 2047 tnext := getExec(n.tnext) 2048 i := n.findex 2049 l := n.level 2050 2051 // Note: 2052 // Here we have to store the result using 2053 // f.data[i] = value(...) 2054 // instead of normal 2055 // dest(f).Set(value(...) 2056 // because the value returned by FieldByIndex() must be preserved 2057 // for possible future Set operations on the struct field (avoid a 2058 // dereference from Set, resulting in setting a copy of the 2059 // original field). 2060 2061 if n.fnext != nil { 2062 fnext := getExec(n.fnext) 2063 n.exec = func(f *frame) bltn { 2064 v := value(f) 2065 r := v.FieldByIndex(index) 2066 getFrame(f, l).data[i] = r 2067 if r.Bool() { 2068 return tnext 2069 } 2070 return fnext 2071 } 2072 } else { 2073 n.exec = func(f *frame) bltn { 2074 v := value(f) 2075 getFrame(f, l).data[i] = v.FieldByIndex(index) 2076 return tnext 2077 } 2078 } 2079 } 2080 2081 func getPtrIndexSeq(n *node) { 2082 index := n.val.([]int) 2083 tnext := getExec(n.tnext) 2084 value := genValue(n.child[0]) 2085 i := n.findex 2086 l := n.level 2087 2088 if n.fnext != nil { 2089 fnext := getExec(n.fnext) 2090 n.exec = func(f *frame) bltn { 2091 r := value(f).Elem().FieldByIndex(index) 2092 getFrame(f, l).data[i] = r 2093 if r.Bool() { 2094 return tnext 2095 } 2096 return fnext 2097 } 2098 } else { 2099 n.exec = func(f *frame) bltn { 2100 getFrame(f, l).data[i] = value(f).Elem().FieldByIndex(index) 2101 return tnext 2102 } 2103 } 2104 } 2105 2106 func getIndexSeqField(n *node) { 2107 value := genValue(n.child[0]) 2108 index := n.val.([]int) 2109 i := n.findex 2110 l := n.level 2111 tnext := getExec(n.tnext) 2112 2113 if n.fnext != nil { 2114 fnext := getExec(n.fnext) 2115 if n.child[0].typ.TypeOf().Kind() == reflect.Ptr { 2116 n.exec = func(f *frame) bltn { 2117 r := value(f).Elem().FieldByIndex(index) 2118 getFrame(f, l).data[i] = r 2119 if r.Bool() { 2120 return tnext 2121 } 2122 return fnext 2123 } 2124 } else { 2125 n.exec = func(f *frame) bltn { 2126 r := value(f).FieldByIndex(index) 2127 getFrame(f, l).data[i] = r 2128 if r.Bool() { 2129 return tnext 2130 } 2131 return fnext 2132 } 2133 } 2134 } else { 2135 if n.child[0].typ.TypeOf().Kind() == reflect.Ptr { 2136 n.exec = func(f *frame) bltn { 2137 getFrame(f, l).data[i] = value(f).Elem().FieldByIndex(index) 2138 return tnext 2139 } 2140 } else { 2141 n.exec = func(f *frame) bltn { 2142 getFrame(f, l).data[i] = value(f).FieldByIndex(index) 2143 return tnext 2144 } 2145 } 2146 } 2147 } 2148 2149 func getIndexSeqPtrMethod(n *node) { 2150 value := genValue(n.child[0]) 2151 index := n.val.([]int) 2152 fi := index[1:] 2153 mi := index[0] 2154 i := n.findex 2155 l := n.level 2156 next := getExec(n.tnext) 2157 2158 if n.child[0].typ.TypeOf().Kind() == reflect.Ptr { 2159 if len(fi) == 0 { 2160 n.exec = func(f *frame) bltn { 2161 getFrame(f, l).data[i] = value(f).Method(mi) 2162 return next 2163 } 2164 } else { 2165 n.exec = func(f *frame) bltn { 2166 getFrame(f, l).data[i] = value(f).Elem().FieldByIndex(fi).Addr().Method(mi) 2167 return next 2168 } 2169 } 2170 } else { 2171 if len(fi) == 0 { 2172 n.exec = func(f *frame) bltn { 2173 getFrame(f, l).data[i] = value(f).Addr().Method(mi) 2174 return next 2175 } 2176 } else { 2177 n.exec = func(f *frame) bltn { 2178 getFrame(f, l).data[i] = value(f).FieldByIndex(fi).Addr().Method(mi) 2179 return next 2180 } 2181 } 2182 } 2183 } 2184 2185 func getIndexSeqMethod(n *node) { 2186 value := genValue(n.child[0]) 2187 index := n.val.([]int) 2188 fi := index[1:] 2189 mi := index[0] 2190 i := n.findex 2191 l := n.level 2192 next := getExec(n.tnext) 2193 2194 if n.child[0].typ.TypeOf().Kind() == reflect.Ptr { 2195 if len(fi) == 0 { 2196 n.exec = func(f *frame) bltn { 2197 getFrame(f, l).data[i] = value(f).Elem().Method(mi) 2198 return next 2199 } 2200 } else { 2201 n.exec = func(f *frame) bltn { 2202 getFrame(f, l).data[i] = value(f).Elem().FieldByIndex(fi).Method(mi) 2203 return next 2204 } 2205 } 2206 } else { 2207 if len(fi) == 0 { 2208 n.exec = func(f *frame) bltn { 2209 getFrame(f, l).data[i] = value(f).Method(mi) 2210 return next 2211 } 2212 } else { 2213 n.exec = func(f *frame) bltn { 2214 getFrame(f, l).data[i] = value(f).FieldByIndex(fi).Method(mi) 2215 return next 2216 } 2217 } 2218 } 2219 } 2220 2221 func neg(n *node) { 2222 dest := genValue(n) 2223 value := genValue(n.child[0]) 2224 next := getExec(n.tnext) 2225 typ := n.typ.concrete().TypeOf() 2226 isInterface := n.typ.TypeOf().Kind() == reflect.Interface 2227 2228 switch n.typ.TypeOf().Kind() { 2229 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 2230 if isInterface { 2231 n.exec = func(f *frame) bltn { 2232 dest(f).Set(reflect.ValueOf(-value(f).Int()).Convert(typ)) 2233 return next 2234 } 2235 return 2236 } 2237 n.exec = func(f *frame) bltn { 2238 dest(f).SetInt(-value(f).Int()) 2239 return next 2240 } 2241 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 2242 if isInterface { 2243 n.exec = func(f *frame) bltn { 2244 dest(f).Set(reflect.ValueOf(-value(f).Uint()).Convert(typ)) 2245 return next 2246 } 2247 return 2248 } 2249 n.exec = func(f *frame) bltn { 2250 dest(f).SetUint(-value(f).Uint()) 2251 return next 2252 } 2253 case reflect.Float32, reflect.Float64: 2254 if isInterface { 2255 n.exec = func(f *frame) bltn { 2256 dest(f).Set(reflect.ValueOf(-value(f).Float()).Convert(typ)) 2257 return next 2258 } 2259 return 2260 } 2261 n.exec = func(f *frame) bltn { 2262 dest(f).SetFloat(-value(f).Float()) 2263 return next 2264 } 2265 case reflect.Complex64, reflect.Complex128: 2266 if isInterface { 2267 n.exec = func(f *frame) bltn { 2268 dest(f).Set(reflect.ValueOf(-value(f).Complex()).Convert(typ)) 2269 return next 2270 } 2271 return 2272 } 2273 n.exec = func(f *frame) bltn { 2274 dest(f).SetComplex(-value(f).Complex()) 2275 return next 2276 } 2277 } 2278 } 2279 2280 func pos(n *node) { 2281 dest := genValue(n) 2282 value := genValue(n.child[0]) 2283 next := getExec(n.tnext) 2284 2285 n.exec = func(f *frame) bltn { 2286 dest(f).Set(value(f)) 2287 return next 2288 } 2289 } 2290 2291 func bitNot(n *node) { 2292 dest := genValue(n) 2293 value := genValue(n.child[0]) 2294 next := getExec(n.tnext) 2295 typ := n.typ.concrete().TypeOf() 2296 isInterface := n.typ.TypeOf().Kind() == reflect.Interface 2297 2298 switch typ.Kind() { 2299 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 2300 if isInterface { 2301 n.exec = func(f *frame) bltn { 2302 dest(f).Set(reflect.ValueOf(^value(f).Int()).Convert(typ)) 2303 return next 2304 } 2305 return 2306 } 2307 n.exec = func(f *frame) bltn { 2308 dest(f).SetInt(^value(f).Int()) 2309 return next 2310 } 2311 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 2312 if isInterface { 2313 n.exec = func(f *frame) bltn { 2314 dest(f).Set(reflect.ValueOf(^value(f).Uint()).Convert(typ)) 2315 return next 2316 } 2317 return 2318 } 2319 n.exec = func(f *frame) bltn { 2320 dest(f).SetUint(^value(f).Uint()) 2321 return next 2322 } 2323 } 2324 } 2325 2326 func land(n *node) { 2327 value0 := genValue(n.child[0]) 2328 value1 := genValue(n.child[1]) 2329 tnext := getExec(n.tnext) 2330 dest := genValue(n) 2331 typ := n.typ.concrete().TypeOf() 2332 isInterface := n.typ.TypeOf().Kind() == reflect.Interface 2333 2334 if n.fnext != nil { 2335 fnext := getExec(n.fnext) 2336 n.exec = func(f *frame) bltn { 2337 if value0(f).Bool() && value1(f).Bool() { 2338 dest(f).SetBool(true) 2339 return tnext 2340 } 2341 dest(f).SetBool(false) 2342 return fnext 2343 } 2344 return 2345 } 2346 if isInterface { 2347 n.exec = func(f *frame) bltn { 2348 dest(f).Set(reflect.ValueOf(value0(f).Bool() && value1(f).Bool()).Convert(typ)) 2349 return tnext 2350 } 2351 return 2352 } 2353 n.exec = func(f *frame) bltn { 2354 dest(f).SetBool(value0(f).Bool() && value1(f).Bool()) 2355 return tnext 2356 } 2357 } 2358 2359 func lor(n *node) { 2360 value0 := genValue(n.child[0]) 2361 value1 := genValue(n.child[1]) 2362 tnext := getExec(n.tnext) 2363 dest := genValue(n) 2364 typ := n.typ.concrete().TypeOf() 2365 isInterface := n.typ.TypeOf().Kind() == reflect.Interface 2366 2367 if n.fnext != nil { 2368 fnext := getExec(n.fnext) 2369 n.exec = func(f *frame) bltn { 2370 if value0(f).Bool() || value1(f).Bool() { 2371 dest(f).SetBool(true) 2372 return tnext 2373 } 2374 dest(f).SetBool(false) 2375 return fnext 2376 } 2377 return 2378 } 2379 if isInterface { 2380 n.exec = func(f *frame) bltn { 2381 dest(f).Set(reflect.ValueOf(value0(f).Bool() || value1(f).Bool()).Convert(typ)) 2382 return tnext 2383 } 2384 return 2385 } 2386 n.exec = func(f *frame) bltn { 2387 dest(f).SetBool(value0(f).Bool() || value1(f).Bool()) 2388 return tnext 2389 } 2390 } 2391 2392 func nop(n *node) { 2393 next := getExec(n.tnext) 2394 2395 n.exec = func(f *frame) bltn { 2396 return next 2397 } 2398 } 2399 2400 func branch(n *node) { 2401 tnext := getExec(n.tnext) 2402 fnext := getExec(n.fnext) 2403 value := genValue(n) 2404 2405 n.exec = func(f *frame) bltn { 2406 if value(f).Bool() { 2407 return tnext 2408 } 2409 return fnext 2410 } 2411 } 2412 2413 func _return(n *node) { 2414 child := n.child 2415 def := n.val.(*node) 2416 values := make([]func(*frame) reflect.Value, len(child)) 2417 for i, c := range child { 2418 switch t := def.typ.ret[i]; t.cat { 2419 case errorT: 2420 values[i] = genInterfaceWrapper(c, t.TypeOf()) 2421 case funcT: 2422 values[i] = genValue(c) 2423 case valueT: 2424 switch t.rtype.Kind() { 2425 case reflect.Interface: 2426 values[i] = genInterfaceWrapper(c, t.TypeOf()) 2427 continue 2428 case reflect.Func: 2429 values[i] = genFunctionWrapper(c) 2430 continue 2431 } 2432 fallthrough 2433 default: 2434 switch { 2435 case isInterfaceSrc(t): 2436 if len(t.field) == 0 { 2437 // empty interface case. 2438 // we can't let genValueInterface deal with it, because we call on c, 2439 // not on n, which means that the interfaceT knowledge is lost. 2440 values[i] = genValue(c) 2441 break 2442 } 2443 values[i] = genValueInterface(c) 2444 case c.typ.untyped: 2445 values[i] = genValueAs(c, t.TypeOf()) 2446 default: 2447 values[i] = genValue(c) 2448 } 2449 } 2450 } 2451 2452 switch len(child) { 2453 case 0: 2454 n.exec = nil 2455 case 1: 2456 switch { 2457 case !child[0].rval.IsValid() && child[0].kind == binaryExpr: 2458 // No additional runtime operation is necessary for constants (not in frame) or 2459 // binary expressions (stored directly at the right location in frame). 2460 n.exec = nil 2461 case isCall(child[0]) && n.child[0].typ.id() == def.typ.ret[0].id(): 2462 // Calls are optmized as long as no type conversion is involved. 2463 n.exec = nil 2464 default: 2465 // Regular return: store the value to return at to start of the frame. 2466 v := values[0] 2467 n.exec = func(f *frame) bltn { 2468 f.data[0].Set(v(f)) 2469 return nil 2470 } 2471 } 2472 case 2: 2473 v0, v1 := values[0], values[1] 2474 n.exec = func(f *frame) bltn { 2475 f.data[0].Set(v0(f)) 2476 f.data[1].Set(v1(f)) 2477 return nil 2478 } 2479 default: 2480 n.exec = func(f *frame) bltn { 2481 for i, value := range values { 2482 f.data[i].Set(value(f)) 2483 } 2484 return nil 2485 } 2486 } 2487 } 2488 2489 func arrayLit(n *node) { 2490 value := valueGenerator(n, n.findex) 2491 next := getExec(n.tnext) 2492 child := n.child 2493 if n.nleft == 1 { 2494 child = n.child[1:] 2495 } 2496 2497 values := make([]func(*frame) reflect.Value, len(child)) 2498 index := make([]int, len(child)) 2499 var max, prev int 2500 2501 ntyp := n.typ.resolveAlias() 2502 for i, c := range child { 2503 if c.kind == keyValueExpr { 2504 values[i] = genDestValue(ntyp.val, c.child[1]) 2505 index[i] = int(vInt(c.child[0].rval)) 2506 } else { 2507 values[i] = genDestValue(ntyp.val, c) 2508 index[i] = prev 2509 } 2510 prev = index[i] + 1 2511 if prev > max { 2512 max = prev 2513 } 2514 } 2515 2516 typ := n.typ.frameType() 2517 kind := typ.Kind() 2518 n.exec = func(f *frame) bltn { 2519 var a reflect.Value 2520 if kind == reflect.Slice { 2521 a = reflect.MakeSlice(typ, max, max) 2522 } else { 2523 a, _ = n.typ.zero() 2524 } 2525 for i, v := range values { 2526 a.Index(index[i]).Set(v(f)) 2527 } 2528 value(f).Set(a) 2529 return next 2530 } 2531 } 2532 2533 func mapLit(n *node) { 2534 value := valueGenerator(n, n.findex) 2535 next := getExec(n.tnext) 2536 child := n.child 2537 if n.nleft == 1 { 2538 child = n.child[1:] 2539 } 2540 typ := n.typ.frameType() 2541 keys := make([]func(*frame) reflect.Value, len(child)) 2542 values := make([]func(*frame) reflect.Value, len(child)) 2543 for i, c := range child { 2544 keys[i] = genDestValue(n.typ.key, c.child[0]) 2545 values[i] = genDestValue(n.typ.val, c.child[1]) 2546 } 2547 2548 n.exec = func(f *frame) bltn { 2549 m := reflect.MakeMap(typ) 2550 for i, k := range keys { 2551 m.SetMapIndex(k(f), values[i](f)) 2552 } 2553 value(f).Set(m) 2554 return next 2555 } 2556 } 2557 2558 func compositeBinMap(n *node) { 2559 value := valueGenerator(n, n.findex) 2560 next := getExec(n.tnext) 2561 child := n.child 2562 if n.nleft == 1 { 2563 child = n.child[1:] 2564 } 2565 typ := n.typ.frameType() 2566 keys := make([]func(*frame) reflect.Value, len(child)) 2567 values := make([]func(*frame) reflect.Value, len(child)) 2568 for i, c := range child { 2569 convertLiteralValue(c.child[0], typ.Key()) 2570 convertLiteralValue(c.child[1], typ.Elem()) 2571 keys[i] = genValue(c.child[0]) 2572 2573 if isFuncSrc(c.child[1].typ) { 2574 values[i] = genFunctionWrapper(c.child[1]) 2575 } else { 2576 values[i] = genValue(c.child[1]) 2577 } 2578 } 2579 2580 n.exec = func(f *frame) bltn { 2581 m := reflect.MakeMap(typ) 2582 for i, k := range keys { 2583 m.SetMapIndex(k(f), values[i](f)) 2584 } 2585 value(f).Set(m) 2586 return next 2587 } 2588 } 2589 2590 func compositeBinSlice(n *node) { 2591 value := valueGenerator(n, n.findex) 2592 next := getExec(n.tnext) 2593 child := n.child 2594 if n.nleft == 1 { 2595 child = n.child[1:] 2596 } 2597 2598 values := make([]func(*frame) reflect.Value, len(child)) 2599 index := make([]int, len(child)) 2600 rtype := n.typ.rtype.Elem() 2601 var max, prev int 2602 2603 for i, c := range child { 2604 if c.kind == keyValueExpr { 2605 convertLiteralValue(c.child[1], rtype) 2606 values[i] = genValue(c.child[1]) 2607 index[i] = int(vInt(c.child[0].rval)) 2608 } else { 2609 convertLiteralValue(c, rtype) 2610 values[i] = genValue(c) 2611 index[i] = prev 2612 } 2613 prev = index[i] + 1 2614 if prev > max { 2615 max = prev 2616 } 2617 } 2618 2619 typ := n.typ.frameType() 2620 kind := typ.Kind() 2621 n.exec = func(f *frame) bltn { 2622 var a reflect.Value 2623 if kind == reflect.Slice { 2624 a = reflect.MakeSlice(typ, max, max) 2625 } else { 2626 a, _ = n.typ.zero() 2627 } 2628 for i, v := range values { 2629 a.Index(index[i]).Set(v(f)) 2630 } 2631 value(f).Set(a) 2632 return next 2633 } 2634 } 2635 2636 // doCompositeBinStruct creates and populates a struct object from a binary type. 2637 func doCompositeBinStruct(n *node, hasType bool) { 2638 next := getExec(n.tnext) 2639 value := valueGenerator(n, n.findex) 2640 typ := n.typ.rtype 2641 if n.typ.cat == ptrT || n.typ.cat == linkedT { 2642 typ = n.typ.val.rtype 2643 } 2644 child := n.child 2645 if hasType { 2646 child = n.child[1:] 2647 } 2648 values := make([]func(*frame) reflect.Value, len(child)) 2649 fieldIndex := make([][]int, len(child)) 2650 for i, c := range child { 2651 if c.kind == keyValueExpr { 2652 if sf, ok := typ.FieldByName(c.child[0].ident); ok { 2653 fieldIndex[i] = sf.Index 2654 convertLiteralValue(c.child[1], sf.Type) 2655 if isFuncSrc(c.child[1].typ) { 2656 values[i] = genFunctionWrapper(c.child[1]) 2657 } else { 2658 values[i] = genValue(c.child[1]) 2659 } 2660 } 2661 } else { 2662 fieldIndex[i] = []int{i} 2663 if isFuncSrc(c.typ) && len(c.child) > 1 { 2664 convertLiteralValue(c.child[1], typ.Field(i).Type) 2665 values[i] = genFunctionWrapper(c.child[1]) 2666 } else { 2667 convertLiteralValue(c, typ.Field(i).Type) 2668 values[i] = genValue(c) 2669 } 2670 } 2671 } 2672 2673 frameIndex := n.findex 2674 l := n.level 2675 2676 n.exec = func(f *frame) bltn { 2677 s := reflect.New(typ).Elem() 2678 for i, v := range values { 2679 s.FieldByIndex(fieldIndex[i]).Set(v(f)) 2680 } 2681 d := value(f) 2682 switch { 2683 case d.Kind() == reflect.Ptr: 2684 d.Set(s.Addr()) 2685 default: 2686 getFrame(f, l).data[frameIndex] = s 2687 } 2688 return next 2689 } 2690 } 2691 2692 func compositeBinStruct(n *node) { doCompositeBinStruct(n, true) } 2693 func compositeBinStructNotype(n *node) { doCompositeBinStruct(n, false) } 2694 2695 func destType(n *node) *itype { 2696 switch n.anc.kind { 2697 case assignStmt, defineStmt: 2698 return n.anc.child[0].typ 2699 default: 2700 return n.typ 2701 } 2702 } 2703 2704 func doComposite(n *node, hasType bool, keyed bool) { 2705 value := valueGenerator(n, n.findex) 2706 next := getExec(n.tnext) 2707 typ := n.typ 2708 if typ.cat == ptrT || typ.cat == linkedT { 2709 typ = typ.val 2710 } 2711 child := n.child 2712 if hasType { 2713 child = n.child[1:] 2714 } 2715 destInterface := isInterfaceSrc(destType(n)) 2716 2717 values := make(map[int]func(*frame) reflect.Value) 2718 for i, c := range child { 2719 var val *node 2720 var fieldIndex int 2721 if keyed { 2722 val = c.child[1] 2723 fieldIndex = typ.fieldIndex(c.child[0].ident) 2724 } else { 2725 val = c 2726 fieldIndex = i 2727 } 2728 ft := typ.field[fieldIndex].typ 2729 rft := ft.TypeOf() 2730 convertLiteralValue(val, rft) 2731 switch { 2732 case val.typ.cat == nilT: 2733 values[fieldIndex] = func(*frame) reflect.Value { return reflect.New(rft).Elem() } 2734 case isNamedFuncSrc(val.typ): 2735 values[fieldIndex] = genValueAsFunctionWrapper(val) 2736 case isInterfaceSrc(ft) && (!isEmptyInterface(ft) || len(val.typ.method) > 0): 2737 values[fieldIndex] = genValueInterface(val) 2738 case isInterface(ft): 2739 values[fieldIndex] = genInterfaceWrapper(val, rft) 2740 default: 2741 values[fieldIndex] = genValue(val) 2742 } 2743 } 2744 2745 frameIndex := n.findex 2746 l := n.level 2747 rt := typ.TypeOf() 2748 2749 n.exec = func(f *frame) bltn { 2750 a := reflect.New(rt).Elem() 2751 for i, v := range values { 2752 a.Field(i).Set(v(f)) 2753 } 2754 d := value(f) 2755 switch { 2756 case d.Kind() == reflect.Ptr: 2757 d.Set(a.Addr()) 2758 case destInterface: 2759 if len(destType(n).field) > 0 { 2760 d.Set(reflect.ValueOf(valueInterface{n, a})) 2761 break 2762 } 2763 d.Set(a) 2764 default: 2765 getFrame(f, l).data[frameIndex] = a 2766 } 2767 return next 2768 } 2769 } 2770 2771 // doCompositeLit creates and populates a struct object. 2772 func doCompositeLit(n *node, hasType bool) { 2773 doComposite(n, hasType, false) 2774 } 2775 2776 func compositeLit(n *node) { doCompositeLit(n, true) } 2777 func compositeLitNotype(n *node) { doCompositeLit(n, false) } 2778 2779 // doCompositeLitKeyed creates a struct Object, filling fields from sparse key-values. 2780 func doCompositeLitKeyed(n *node, hasType bool) { 2781 doComposite(n, hasType, true) 2782 } 2783 2784 func compositeLitKeyed(n *node) { doCompositeLitKeyed(n, true) } 2785 func compositeLitKeyedNotype(n *node) { doCompositeLitKeyed(n, false) } 2786 2787 func empty(n *node) {} 2788 2789 var rat = reflect.ValueOf((*[]rune)(nil)).Type().Elem() // runes array type 2790 2791 func _range(n *node) { 2792 index0 := n.child[0].findex // array index location in frame 2793 index2 := index0 - 1 // shallow array for range, always just behind index0 2794 index3 := index2 - 1 // additional location to store string char position 2795 fnext := getExec(n.fnext) 2796 tnext := getExec(n.tnext) 2797 2798 var value func(*frame) reflect.Value 2799 var an *node 2800 if len(n.child) == 4 { 2801 an = n.child[2] 2802 index1 := n.child[1].findex // array value location in frame 2803 if isString(an.typ.TypeOf()) { 2804 // Special variant of "range" for string, where the index indicates the byte position 2805 // of the rune in the string, rather than the index of the rune in array. 2806 stringType := reflect.TypeOf("") 2807 value = genValueAs(an, rat) // range on string iterates over runes 2808 n.exec = func(f *frame) bltn { 2809 a := f.data[index2] 2810 v0 := f.data[index3] 2811 v0.SetInt(v0.Int() + 1) 2812 i := int(v0.Int()) 2813 if i >= a.Len() { 2814 return fnext 2815 } 2816 // Compute byte position of the rune in string 2817 pos := a.Slice(0, i).Convert(stringType).Len() 2818 f.data[index0].SetInt(int64(pos)) 2819 f.data[index1].Set(a.Index(i)) 2820 return tnext 2821 } 2822 } else { 2823 value = genValueRangeArray(an) 2824 n.exec = func(f *frame) bltn { 2825 a := f.data[index2] 2826 v0 := f.data[index0] 2827 v0.SetInt(v0.Int() + 1) 2828 i := int(v0.Int()) 2829 if i >= a.Len() { 2830 return fnext 2831 } 2832 f.data[index1].Set(a.Index(i)) 2833 return tnext 2834 } 2835 } 2836 } else { 2837 an = n.child[1] 2838 if isString(an.typ.TypeOf()) { 2839 value = genValueAs(an, rat) // range on string iterates over runes 2840 } else { 2841 value = genValueRangeArray(an) 2842 } 2843 n.exec = func(f *frame) bltn { 2844 v0 := f.data[index0] 2845 v0.SetInt(v0.Int() + 1) 2846 if int(v0.Int()) >= f.data[index2].Len() { 2847 return fnext 2848 } 2849 return tnext 2850 } 2851 } 2852 2853 // Init sequence 2854 next := n.exec 2855 index := index0 2856 if isString(an.typ.TypeOf()) && len(n.child) == 4 { 2857 index = index3 2858 } 2859 n.child[0].exec = func(f *frame) bltn { 2860 f.data[index2] = value(f) // set array shallow copy for range 2861 f.data[index].SetInt(-1) // assing index value 2862 return next 2863 } 2864 } 2865 2866 func rangeChan(n *node) { 2867 i := n.child[0].findex // element index location in frame 2868 value := genValue(n.child[1]) // chan 2869 fnext := getExec(n.fnext) 2870 tnext := getExec(n.tnext) 2871 2872 n.exec = func(f *frame) bltn { 2873 f.mutex.RLock() 2874 done := f.done 2875 f.mutex.RUnlock() 2876 2877 chosen, v, ok := reflect.Select([]reflect.SelectCase{done, {Dir: reflect.SelectRecv, Chan: value(f)}}) 2878 if chosen == 0 { 2879 return nil 2880 } 2881 if !ok { 2882 return fnext 2883 } 2884 f.data[i].Set(v) 2885 return tnext 2886 } 2887 } 2888 2889 func rangeMap(n *node) { 2890 index0 := n.child[0].findex // map index location in frame 2891 index2 := index0 - 1 // iterator for range, always just behind index0 2892 fnext := getExec(n.fnext) 2893 tnext := getExec(n.tnext) 2894 2895 var value func(*frame) reflect.Value 2896 if len(n.child) == 4 { 2897 index1 := n.child[1].findex // map value location in frame 2898 value = genValue(n.child[2]) // map 2899 n.exec = func(f *frame) bltn { 2900 iter := f.data[index2].Interface().(*reflect.MapIter) 2901 if !iter.Next() { 2902 return fnext 2903 } 2904 f.data[index0].Set(iter.Key()) 2905 f.data[index1].Set(iter.Value()) 2906 return tnext 2907 } 2908 } else { 2909 value = genValue(n.child[1]) // map 2910 n.exec = func(f *frame) bltn { 2911 iter := f.data[index2].Interface().(*reflect.MapIter) 2912 if !iter.Next() { 2913 return fnext 2914 } 2915 f.data[index0].Set(iter.Key()) 2916 return tnext 2917 } 2918 } 2919 2920 // Init sequence 2921 next := n.exec 2922 n.child[0].exec = func(f *frame) bltn { 2923 f.data[index2].Set(reflect.ValueOf(value(f).MapRange())) 2924 return next 2925 } 2926 } 2927 2928 func _case(n *node) { 2929 tnext := getExec(n.tnext) 2930 2931 // TODO(mpl): a lot of what is done in typeAssert should probably be redone/reused here. 2932 switch { 2933 case n.anc.anc.kind == typeSwitch: 2934 fnext := getExec(n.fnext) 2935 sn := n.anc.anc // switch node 2936 types := make([]*itype, len(n.child)-1) 2937 for i := range types { 2938 types[i] = n.child[i].typ 2939 } 2940 srcValue := genValue(sn.child[1].lastChild().child[0]) 2941 2942 if len(sn.child[1].child) != 2 { 2943 // no assign in switch guard 2944 if len(n.child) <= 1 { 2945 n.exec = func(f *frame) bltn { return tnext } 2946 } else { 2947 n.exec = func(f *frame) bltn { 2948 ival := srcValue(f).Interface() 2949 val, ok := ival.(valueInterface) 2950 // TODO(mpl): I'm assuming here that !ok means that we're dealing with the empty 2951 // interface case. But maybe we should make sure by checking the relevant cat 2952 // instead? later. Use t := v.Type(); t.Kind() == reflect.Interface , like above. 2953 if !ok { 2954 var stype string 2955 if ival != nil { 2956 stype = strings.ReplaceAll(reflect.TypeOf(ival).String(), " {}", "{}") 2957 } 2958 for _, typ := range types { 2959 // TODO(mpl): we should actually use canAssertTypes, but need to find a valid 2960 // rtype for typ. Plus we need to refactor with typeAssert(). 2961 // weak check instead for now. 2962 if ival == nil { 2963 if typ.cat == nilT { 2964 return tnext 2965 } 2966 continue 2967 } 2968 if stype == typ.id() { 2969 return tnext 2970 } 2971 } 2972 return fnext 2973 } 2974 if v := val.node; v != nil { 2975 for _, typ := range types { 2976 if v.typ.id() == typ.id() { 2977 return tnext 2978 } 2979 } 2980 } 2981 return fnext 2982 } 2983 } 2984 break 2985 } 2986 2987 // assign in switch guard 2988 destValue := genValue(n.lastChild().child[0]) 2989 switch len(types) { 2990 case 0: 2991 // default clause: assign var to interface value 2992 n.exec = func(f *frame) bltn { 2993 destValue(f).Set(srcValue(f)) 2994 return tnext 2995 } 2996 case 1: 2997 // match against 1 type: assign var to concrete value 2998 typ := types[0] 2999 n.exec = func(f *frame) bltn { 3000 v := srcValue(f) 3001 if !v.IsValid() { 3002 // match zero value against nil 3003 if typ.cat == nilT { 3004 return tnext 3005 } 3006 return fnext 3007 } 3008 if t := v.Type(); t.Kind() == reflect.Interface { 3009 if typ.cat == nilT && v.IsNil() { 3010 return tnext 3011 } 3012 rtyp := typ.TypeOf() 3013 if rtyp == nil { 3014 return fnext 3015 } 3016 elem := v.Elem() 3017 if rtyp.String() == t.String() && implementsInterface(v, typ) { 3018 destValue(f).Set(elem) 3019 return tnext 3020 } 3021 ival := v.Interface() 3022 if ival != nil && rtyp.String() == reflect.TypeOf(ival).String() { 3023 destValue(f).Set(elem) 3024 return tnext 3025 } 3026 if typ.cat == valueT && rtyp.Kind() == reflect.Interface && elem.IsValid() && elem.Type().Implements(rtyp) { 3027 destValue(f).Set(elem) 3028 return tnext 3029 } 3030 return fnext 3031 } 3032 if vi, ok := v.Interface().(valueInterface); ok { 3033 if vi.node != nil { 3034 if vi.node.typ.id() == typ.id() { 3035 destValue(f).Set(vi.value) 3036 return tnext 3037 } 3038 } 3039 return fnext 3040 } 3041 if v.Type() == typ.TypeOf() { 3042 destValue(f).Set(v) 3043 return tnext 3044 } 3045 return fnext 3046 } 3047 3048 default: 3049 n.exec = func(f *frame) bltn { 3050 val := srcValue(f) 3051 if t := val.Type(); t.Kind() == reflect.Interface { 3052 for _, typ := range types { 3053 if typ.cat == nilT && val.IsNil() { 3054 return tnext 3055 } 3056 rtyp := typ.TypeOf() 3057 if rtyp == nil { 3058 continue 3059 } 3060 elem := val.Elem() 3061 if rtyp.String() == t.String() && implementsInterface(val, typ) { 3062 destValue(f).Set(elem) 3063 return tnext 3064 } 3065 ival := val.Interface() 3066 if ival != nil && rtyp.String() == reflect.TypeOf(ival).String() { 3067 destValue(f).Set(elem) 3068 return tnext 3069 } 3070 if typ.cat == valueT && rtyp.Kind() == reflect.Interface && elem.IsValid() && elem.Type().Implements(rtyp) { 3071 destValue(f).Set(elem) 3072 return tnext 3073 } 3074 } 3075 return fnext 3076 } 3077 if vi, ok := val.Interface().(valueInterface); ok { 3078 if v := vi.node; v != nil { 3079 for _, typ := range types { 3080 if v.typ.id() == typ.id() { 3081 destValue(f).Set(val) 3082 return tnext 3083 } 3084 } 3085 } 3086 return fnext 3087 } 3088 vt := val.Type() 3089 for _, typ := range types { 3090 if vt == typ.TypeOf() { 3091 destValue(f).Set(val) 3092 return tnext 3093 } 3094 } 3095 return fnext 3096 } 3097 } 3098 3099 case len(n.child) <= 1: // default clause 3100 n.exec = func(f *frame) bltn { return tnext } 3101 3102 default: 3103 fnext := getExec(n.fnext) 3104 l := len(n.anc.anc.child) 3105 value := genValue(n.anc.anc.child[l-2]) 3106 values := make([]func(*frame) reflect.Value, len(n.child)-1) 3107 for i := range values { 3108 values[i] = genValue(n.child[i]) 3109 } 3110 n.exec = func(f *frame) bltn { 3111 v0 := value(f) 3112 for _, v := range values { 3113 v1 := v(f) 3114 if !v0.Type().AssignableTo(v1.Type()) { 3115 v0 = v0.Convert(v1.Type()) 3116 } 3117 if v0.Interface() == v1.Interface() { 3118 return tnext 3119 } 3120 } 3121 return fnext 3122 } 3123 } 3124 } 3125 3126 func implementsInterface(v reflect.Value, t *itype) bool { 3127 rt := v.Type() 3128 if t.cat == valueT { 3129 return rt.Implements(t.rtype) 3130 } 3131 vt := &itype{cat: valueT, rtype: rt} 3132 if vt.methods().contains(t.methods()) { 3133 return true 3134 } 3135 vi, ok := v.Interface().(valueInterface) 3136 if !ok { 3137 return false 3138 } 3139 return vi.node != nil && vi.node.typ.methods().contains(t.methods()) 3140 } 3141 3142 func appendSlice(n *node) { 3143 dest := genValueOutput(n, n.typ.rtype) 3144 next := getExec(n.tnext) 3145 value := genValue(n.child[1]) 3146 value0 := genValue(n.child[2]) 3147 3148 if isString(n.child[2].typ.TypeOf()) { 3149 typ := reflect.TypeOf([]byte{}) 3150 n.exec = func(f *frame) bltn { 3151 dest(f).Set(reflect.AppendSlice(value(f), value0(f).Convert(typ))) 3152 return next 3153 } 3154 } else { 3155 n.exec = func(f *frame) bltn { 3156 dest(f).Set(reflect.AppendSlice(value(f), value0(f))) 3157 return next 3158 } 3159 } 3160 } 3161 3162 func _append(n *node) { 3163 if len(n.child) == 3 { 3164 c1, c2 := n.child[1], n.child[2] 3165 if (c1.typ.cat == valueT || c2.typ.cat == valueT) && c1.typ.rtype == c2.typ.rtype || 3166 isArray(c2.typ) && c2.typ.elem().id() == n.typ.elem().id() || 3167 isByteArray(c1.typ.TypeOf()) && isString(c2.typ.TypeOf()) { 3168 appendSlice(n) 3169 return 3170 } 3171 } 3172 3173 dest := genValueOutput(n, n.typ.rtype) 3174 value := genValue(n.child[1]) 3175 next := getExec(n.tnext) 3176 3177 switch l := len(n.child); { 3178 case l == 2: 3179 n.exec = func(f *frame) bltn { 3180 dest(f).Set(value(f)) 3181 return next 3182 } 3183 case l > 3: 3184 args := n.child[2:] 3185 l := len(args) 3186 values := make([]func(*frame) reflect.Value, l) 3187 for i, arg := range args { 3188 switch elem := n.typ.elem(); { 3189 case isInterfaceSrc(elem) && (!isEmptyInterface(elem) || len(arg.typ.method) > 0): 3190 values[i] = genValueInterface(arg) 3191 case isInterfaceBin(elem): 3192 values[i] = genInterfaceWrapper(arg, elem.rtype) 3193 case arg.typ.untyped: 3194 values[i] = genValueAs(arg, n.child[1].typ.TypeOf().Elem()) 3195 default: 3196 values[i] = genValue(arg) 3197 } 3198 } 3199 3200 n.exec = func(f *frame) bltn { 3201 sl := make([]reflect.Value, l) 3202 for i, v := range values { 3203 sl[i] = v(f) 3204 } 3205 dest(f).Set(reflect.Append(value(f), sl...)) 3206 return next 3207 } 3208 default: 3209 var value0 func(*frame) reflect.Value 3210 switch elem := n.typ.elem(); { 3211 case isInterfaceSrc(elem) && (!isEmptyInterface(elem) || len(n.child[2].typ.method) > 0): 3212 value0 = genValueInterface(n.child[2]) 3213 case isInterfaceBin(elem): 3214 value0 = genInterfaceWrapper(n.child[2], elem.rtype) 3215 case n.child[2].typ.untyped: 3216 value0 = genValueAs(n.child[2], n.child[1].typ.TypeOf().Elem()) 3217 default: 3218 value0 = genValue(n.child[2]) 3219 } 3220 3221 n.exec = func(f *frame) bltn { 3222 dest(f).Set(reflect.Append(value(f), value0(f))) 3223 return next 3224 } 3225 } 3226 } 3227 3228 func _cap(n *node) { 3229 dest := genValueOutput(n, reflect.TypeOf(int(0))) 3230 value := genValue(n.child[1]) 3231 next := getExec(n.tnext) 3232 3233 if wantEmptyInterface(n) { 3234 n.exec = func(f *frame) bltn { 3235 dest(f).Set(reflect.ValueOf(value(f).Cap())) 3236 return next 3237 } 3238 return 3239 } 3240 n.exec = func(f *frame) bltn { 3241 dest(f).SetInt(int64(value(f).Cap())) 3242 return next 3243 } 3244 } 3245 3246 func _copy(n *node) { 3247 in := []func(*frame) reflect.Value{genValueArray(n.child[1]), genValue(n.child[2])} 3248 out := []func(*frame) reflect.Value{genValueOutput(n, reflect.TypeOf(0))} 3249 3250 genBuiltinDeferWrapper(n, in, out, func(args []reflect.Value) []reflect.Value { 3251 cnt := reflect.Copy(args[0], args[1]) 3252 return []reflect.Value{reflect.ValueOf(cnt)} 3253 }) 3254 } 3255 3256 func _close(n *node) { 3257 in := []func(*frame) reflect.Value{genValue(n.child[1])} 3258 3259 genBuiltinDeferWrapper(n, in, nil, func(args []reflect.Value) []reflect.Value { 3260 args[0].Close() 3261 return nil 3262 }) 3263 } 3264 3265 func _complex(n *node) { 3266 dest := genValueOutput(n, reflect.TypeOf(complex(0, 0))) 3267 c1, c2 := n.child[1], n.child[2] 3268 convertLiteralValue(c1, floatType) 3269 convertLiteralValue(c2, floatType) 3270 value0 := genValue(c1) 3271 value1 := genValue(c2) 3272 next := getExec(n.tnext) 3273 3274 typ := n.typ.TypeOf() 3275 if isComplex(typ) { 3276 if wantEmptyInterface(n) { 3277 n.exec = func(f *frame) bltn { 3278 dest(f).Set(reflect.ValueOf(complex(value0(f).Float(), value1(f).Float()))) 3279 return next 3280 } 3281 return 3282 } 3283 n.exec = func(f *frame) bltn { 3284 dest(f).SetComplex(complex(value0(f).Float(), value1(f).Float())) 3285 return next 3286 } 3287 return 3288 } 3289 // Not a complex type: ignore imaginary part 3290 n.exec = func(f *frame) bltn { 3291 dest(f).Set(value0(f).Convert(typ)) 3292 return next 3293 } 3294 } 3295 3296 func _imag(n *node) { 3297 dest := genValueOutput(n, reflect.TypeOf(float64(0))) 3298 convertLiteralValue(n.child[1], complexType) 3299 value := genValue(n.child[1]) 3300 next := getExec(n.tnext) 3301 3302 if wantEmptyInterface(n) { 3303 n.exec = func(f *frame) bltn { 3304 dest(f).Set(reflect.ValueOf(imag(value(f).Complex()))) 3305 return next 3306 } 3307 return 3308 } 3309 n.exec = func(f *frame) bltn { 3310 dest(f).SetFloat(imag(value(f).Complex())) 3311 return next 3312 } 3313 } 3314 3315 func _real(n *node) { 3316 dest := genValueOutput(n, reflect.TypeOf(float64(0))) 3317 convertLiteralValue(n.child[1], complexType) 3318 value := genValue(n.child[1]) 3319 next := getExec(n.tnext) 3320 3321 if wantEmptyInterface(n) { 3322 n.exec = func(f *frame) bltn { 3323 dest(f).Set(reflect.ValueOf(real(value(f).Complex()))) 3324 return next 3325 } 3326 return 3327 } 3328 n.exec = func(f *frame) bltn { 3329 dest(f).SetFloat(real(value(f).Complex())) 3330 return next 3331 } 3332 } 3333 3334 func _delete(n *node) { 3335 value0 := genValue(n.child[1]) // map 3336 value1 := genValue(n.child[2]) // key 3337 in := []func(*frame) reflect.Value{value0, value1} 3338 var z reflect.Value 3339 3340 genBuiltinDeferWrapper(n, in, nil, func(args []reflect.Value) []reflect.Value { 3341 args[0].SetMapIndex(args[1], z) 3342 return nil 3343 }) 3344 } 3345 3346 func capConst(n *node) { 3347 // There is no Cap() method for reflect.Type, just return Len() instead. 3348 lenConst(n) 3349 } 3350 3351 func lenConst(n *node) { 3352 n.rval = reflect.New(reflect.TypeOf(int(0))).Elem() 3353 c1 := n.child[1] 3354 if c1.rval.IsValid() { 3355 n.rval.SetInt(int64(len(vString(c1.rval)))) 3356 return 3357 } 3358 t := c1.typ.TypeOf() 3359 for t.Kind() == reflect.Ptr { 3360 t = t.Elem() 3361 } 3362 n.rval.SetInt(int64(t.Len())) 3363 } 3364 3365 func _len(n *node) { 3366 dest := genValueOutput(n, reflect.TypeOf(int(0))) 3367 value := genValue(n.child[1]) 3368 if isPtr(n.child[1].typ) { 3369 val := value 3370 value = func(f *frame) reflect.Value { 3371 v := val(f).Elem() 3372 for v.Kind() == reflect.Ptr { 3373 v = v.Elem() 3374 } 3375 return v 3376 } 3377 } 3378 next := getExec(n.tnext) 3379 3380 if wantEmptyInterface(n) { 3381 n.exec = func(f *frame) bltn { 3382 dest(f).Set(reflect.ValueOf(value(f).Len())) 3383 return next 3384 } 3385 return 3386 } 3387 n.exec = func(f *frame) bltn { 3388 dest(f).SetInt(int64(value(f).Len())) 3389 return next 3390 } 3391 } 3392 3393 func _new(n *node) { 3394 next := getExec(n.tnext) 3395 t1 := n.child[1].typ 3396 typ := t1.TypeOf() 3397 dest := genValueOutput(n, reflect.PtrTo(typ)) 3398 3399 if isInterfaceSrc(t1) && (!isEmptyInterface(t1) || len(t1.method) > 0) { 3400 typ = zeroInterfaceValue().Type() 3401 } 3402 3403 n.exec = func(f *frame) bltn { 3404 v := reflect.New(typ) 3405 if vi, ok := v.Interface().(*valueInterface); ok { 3406 vi.node = n 3407 } 3408 dest(f).Set(v) 3409 return next 3410 } 3411 } 3412 3413 // _make allocates and initializes a slice, a map or a chan. 3414 func _make(n *node) { 3415 next := getExec(n.tnext) 3416 typ := n.child[1].typ.frameType() 3417 dest := genValueOutput(n, typ) 3418 3419 switch typ.Kind() { 3420 case reflect.Array, reflect.Slice: 3421 value := genValue(n.child[2]) 3422 3423 switch len(n.child) { 3424 case 3: 3425 n.exec = func(f *frame) bltn { 3426 length := int(vInt(value(f))) 3427 dest(f).Set(reflect.MakeSlice(typ, length, length)) 3428 return next 3429 } 3430 case 4: 3431 value1 := genValue(n.child[3]) 3432 n.exec = func(f *frame) bltn { 3433 dest(f).Set(reflect.MakeSlice(typ, int(vInt(value(f))), int(vInt(value1(f))))) 3434 return next 3435 } 3436 } 3437 3438 case reflect.Chan: 3439 switch len(n.child) { 3440 case 2: 3441 n.exec = func(f *frame) bltn { 3442 dest(f).Set(reflect.MakeChan(typ, 0)) 3443 return next 3444 } 3445 case 3: 3446 value := genValue(n.child[2]) 3447 n.exec = func(f *frame) bltn { 3448 dest(f).Set(reflect.MakeChan(typ, int(vInt(value(f))))) 3449 return next 3450 } 3451 } 3452 3453 case reflect.Map: 3454 switch len(n.child) { 3455 case 2: 3456 n.exec = func(f *frame) bltn { 3457 dest(f).Set(reflect.MakeMap(typ)) 3458 return next 3459 } 3460 case 3: 3461 value := genValue(n.child[2]) 3462 n.exec = func(f *frame) bltn { 3463 dest(f).Set(reflect.MakeMapWithSize(typ, int(vInt(value(f))))) 3464 return next 3465 } 3466 } 3467 } 3468 } 3469 3470 func reset(n *node) { 3471 next := getExec(n.tnext) 3472 3473 switch l := len(n.child) - 1; l { 3474 case 1: 3475 typ := n.child[0].typ.frameType() 3476 i := n.child[0].findex 3477 n.exec = func(f *frame) bltn { 3478 f.data[i] = reflect.New(typ).Elem() 3479 return next 3480 } 3481 case 2: 3482 c0, c1 := n.child[0], n.child[1] 3483 i0, i1 := c0.findex, c1.findex 3484 t0, t1 := c0.typ.frameType(), c1.typ.frameType() 3485 n.exec = func(f *frame) bltn { 3486 f.data[i0] = reflect.New(t0).Elem() 3487 f.data[i1] = reflect.New(t1).Elem() 3488 return next 3489 } 3490 default: 3491 types := make([]reflect.Type, l) 3492 index := make([]int, l) 3493 for i, c := range n.child[:l] { 3494 index[i] = c.findex 3495 types[i] = c.typ.frameType() 3496 } 3497 n.exec = func(f *frame) bltn { 3498 for i, ind := range index { 3499 f.data[ind] = reflect.New(types[i]).Elem() 3500 } 3501 return next 3502 } 3503 } 3504 } 3505 3506 // recv reads from a channel. 3507 func recv(n *node) { 3508 value := genValue(n.child[0]) 3509 tnext := getExec(n.tnext) 3510 i := n.findex 3511 l := n.level 3512 3513 if n.interp.cancelChan { 3514 // Cancellable channel read 3515 if n.fnext != nil { 3516 fnext := getExec(n.fnext) 3517 n.exec = func(f *frame) bltn { 3518 // Fast: channel read doesn't block 3519 ch := value(f) 3520 if r, ok := ch.TryRecv(); ok { 3521 getFrame(f, l).data[i] = r 3522 if r.Bool() { 3523 return tnext 3524 } 3525 return fnext 3526 } 3527 // Slow: channel read blocks, allow cancel 3528 f.mutex.RLock() 3529 done := f.done 3530 f.mutex.RUnlock() 3531 3532 chosen, v, _ := reflect.Select([]reflect.SelectCase{done, {Dir: reflect.SelectRecv, Chan: ch}}) 3533 if chosen == 0 { 3534 return nil 3535 } 3536 if v.Bool() { 3537 return tnext 3538 } 3539 return fnext 3540 } 3541 } else { 3542 n.exec = func(f *frame) bltn { 3543 // Fast: channel read doesn't block 3544 ch := value(f) 3545 if r, ok := ch.TryRecv(); ok { 3546 getFrame(f, l).data[i] = r 3547 return tnext 3548 } 3549 // Slow: channel is blocked, allow cancel 3550 f.mutex.RLock() 3551 done := f.done 3552 f.mutex.RUnlock() 3553 3554 var chosen int 3555 chosen, getFrame(f, l).data[i], _ = reflect.Select([]reflect.SelectCase{done, {Dir: reflect.SelectRecv, Chan: ch}}) 3556 if chosen == 0 { 3557 return nil 3558 } 3559 return tnext 3560 } 3561 } 3562 } else { 3563 // Blocking channel read (less overhead) 3564 if n.fnext != nil { 3565 fnext := getExec(n.fnext) 3566 n.exec = func(f *frame) bltn { 3567 if r, _ := value(f).Recv(); r.Bool() { 3568 getFrame(f, l).data[i] = r 3569 return tnext 3570 } 3571 return fnext 3572 } 3573 } else { 3574 i := n.findex 3575 n.exec = func(f *frame) bltn { 3576 getFrame(f, l).data[i], _ = value(f).Recv() 3577 return tnext 3578 } 3579 } 3580 } 3581 } 3582 3583 func recv2(n *node) { 3584 vchan := genValue(n.child[0]) // chan 3585 vres := genValue(n.anc.child[0]) // result 3586 vok := genValue(n.anc.child[1]) // status 3587 tnext := getExec(n.tnext) 3588 3589 if n.interp.cancelChan { 3590 // Cancellable channel read 3591 n.exec = func(f *frame) bltn { 3592 ch, result, status := vchan(f), vres(f), vok(f) 3593 // Fast: channel read doesn't block 3594 if v, ok := ch.TryRecv(); ok { 3595 result.Set(v) 3596 status.SetBool(true) 3597 return tnext 3598 } 3599 // Slow: channel is blocked, allow cancel 3600 f.mutex.RLock() 3601 done := f.done 3602 f.mutex.RUnlock() 3603 3604 chosen, v, ok := reflect.Select([]reflect.SelectCase{done, {Dir: reflect.SelectRecv, Chan: ch}}) 3605 if chosen == 0 { 3606 return nil 3607 } 3608 result.Set(v) 3609 status.SetBool(ok) 3610 return tnext 3611 } 3612 } else { 3613 // Blocking channel read (less overhead) 3614 n.exec = func(f *frame) bltn { 3615 v, ok := vchan(f).Recv() 3616 vres(f).Set(v) 3617 vok(f).SetBool(ok) 3618 return tnext 3619 } 3620 } 3621 } 3622 3623 func convertLiteralValue(n *node, t reflect.Type) { 3624 switch { 3625 case n.typ.cat == nilT: 3626 // Create a zero value of target type. 3627 n.rval = reflect.New(t).Elem() 3628 case !(n.kind == basicLit || n.rval.IsValid()) || t == nil || t.Kind() == reflect.Interface || t == valueInterfaceType || t.Kind() == reflect.Slice && t.Elem().Kind() == reflect.Interface: 3629 // Skip non-constant values, undefined target type or interface target type. 3630 case n.rval.IsValid(): 3631 // Convert constant value to target type. 3632 convertConstantValue(n) 3633 n.rval = n.rval.Convert(t) 3634 default: 3635 // Create a zero value of target type. 3636 n.rval = reflect.New(t).Elem() 3637 } 3638 } 3639 3640 func convertConstantValue(n *node) { 3641 if !n.rval.IsValid() { 3642 return 3643 } 3644 c, ok := n.rval.Interface().(constant.Value) 3645 if !ok { 3646 return 3647 } 3648 3649 var v reflect.Value 3650 3651 switch c.Kind() { 3652 case constant.Bool: 3653 v = reflect.ValueOf(constant.BoolVal(c)) 3654 case constant.String: 3655 v = reflect.ValueOf(constant.StringVal(c)) 3656 case constant.Int: 3657 i, x := constant.Int64Val(c) 3658 if !x { 3659 panic(n.cfgErrorf("constant %s overflows int64", c.ExactString())) 3660 } 3661 v = reflect.ValueOf(int(i)) 3662 case constant.Float: 3663 f, _ := constant.Float64Val(c) 3664 v = reflect.ValueOf(f) 3665 case constant.Complex: 3666 r, _ := constant.Float64Val(constant.Real(c)) 3667 i, _ := constant.Float64Val(constant.Imag(c)) 3668 v = reflect.ValueOf(complex(r, i)) 3669 } 3670 3671 n.rval = v.Convert(n.typ.TypeOf()) 3672 } 3673 3674 // Write to a channel. 3675 func send(n *node) { 3676 next := getExec(n.tnext) 3677 c0, c1 := n.child[0], n.child[1] 3678 value0 := genValue(c0) // Send channel. 3679 value1 := genDestValue(c0.typ.val, c1) 3680 3681 if !n.interp.cancelChan { 3682 // Send is non-cancellable, has the least overhead. 3683 n.exec = func(f *frame) bltn { 3684 value0(f).Send(value1(f)) 3685 return next 3686 } 3687 return 3688 } 3689 3690 // Send is cancellable, may have some overhead. 3691 n.exec = func(f *frame) bltn { 3692 ch, data := value0(f), value1(f) 3693 // Fast: send on channel doesn't block. 3694 if ok := ch.TrySend(data); ok { 3695 return next 3696 } 3697 // Slow: send on channel blocks, allow cancel. 3698 f.mutex.RLock() 3699 done := f.done 3700 f.mutex.RUnlock() 3701 3702 chosen, _, _ := reflect.Select([]reflect.SelectCase{done, {Dir: reflect.SelectSend, Chan: ch, Send: data}}) 3703 if chosen == 0 { 3704 return nil 3705 } 3706 return next 3707 } 3708 } 3709 3710 func clauseChanDir(n *node) (*node, *node, *node, reflect.SelectDir) { 3711 dir := reflect.SelectDefault 3712 var nod, assigned, ok *node 3713 var stop bool 3714 3715 n.Walk(func(m *node) bool { 3716 switch m.action { 3717 case aRecv: 3718 dir = reflect.SelectRecv 3719 nod = m.child[0] 3720 switch m.anc.action { 3721 case aAssign: 3722 assigned = m.anc.child[0] 3723 case aAssignX: 3724 assigned = m.anc.child[0] 3725 ok = m.anc.child[1] 3726 } 3727 stop = true 3728 case aSend: 3729 dir = reflect.SelectSend 3730 nod = m.child[0] 3731 assigned = m.child[1] 3732 stop = true 3733 } 3734 return !stop 3735 }, nil) 3736 return nod, assigned, ok, dir 3737 } 3738 3739 func _select(n *node) { 3740 nbClause := len(n.child) 3741 chans := make([]*node, nbClause) 3742 assigned := make([]*node, nbClause) 3743 ok := make([]*node, nbClause) 3744 clause := make([]bltn, nbClause) 3745 chanValues := make([]func(*frame) reflect.Value, nbClause) 3746 assignedValues := make([]func(*frame) reflect.Value, nbClause) 3747 okValues := make([]func(*frame) reflect.Value, nbClause) 3748 cases := make([]reflect.SelectCase, nbClause+1) 3749 next := getExec(n.tnext) 3750 3751 for i := 0; i < nbClause; i++ { 3752 cl := n.child[i] 3753 if cl.kind == commClauseDefault { 3754 cases[i].Dir = reflect.SelectDefault 3755 if len(cl.child) == 0 { 3756 clause[i] = func(*frame) bltn { return next } 3757 } else { 3758 clause[i] = getExec(cl.child[0].start) 3759 } 3760 continue 3761 } 3762 // The comm clause is in send or recv direction. 3763 switch c0 := cl.child[0]; { 3764 case len(cl.child) > 1: 3765 // The comm clause contains a channel operation and a clause body. 3766 clause[i] = getExec(cl.child[1].start) 3767 chans[i], assigned[i], ok[i], cases[i].Dir = clauseChanDir(c0) 3768 chanValues[i] = genValue(chans[i]) 3769 if assigned[i] != nil { 3770 assignedValues[i] = genValue(assigned[i]) 3771 } 3772 if ok[i] != nil { 3773 okValues[i] = genValue(ok[i]) 3774 } 3775 case c0.kind == exprStmt && len(c0.child) == 1 && c0.child[0].action == aRecv: 3776 // The comm clause has an empty body clause after channel receive. 3777 chanValues[i] = genValue(c0.child[0].child[0]) 3778 cases[i].Dir = reflect.SelectRecv 3779 clause[i] = func(*frame) bltn { return next } 3780 case c0.kind == sendStmt: 3781 // The comm clause as an empty body clause after channel send. 3782 chanValues[i] = genValue(c0.child[0]) 3783 cases[i].Dir = reflect.SelectSend 3784 assignedValues[i] = genValue(c0.child[1]) 3785 clause[i] = func(*frame) bltn { return next } 3786 } 3787 } 3788 3789 n.exec = func(f *frame) bltn { 3790 f.mutex.RLock() 3791 cases[nbClause] = f.done 3792 f.mutex.RUnlock() 3793 3794 for i := range cases[:nbClause] { 3795 switch cases[i].Dir { 3796 case reflect.SelectRecv: 3797 cases[i].Chan = chanValues[i](f) 3798 case reflect.SelectSend: 3799 cases[i].Chan = chanValues[i](f) 3800 cases[i].Send = assignedValues[i](f) 3801 case reflect.SelectDefault: 3802 // Keep zero values for comm clause 3803 } 3804 } 3805 j, v, s := reflect.Select(cases) 3806 if j == nbClause { 3807 return nil 3808 } 3809 if cases[j].Dir == reflect.SelectRecv && assignedValues[j] != nil { 3810 assignedValues[j](f).Set(v) 3811 if ok[j] != nil { 3812 okValues[j](f).SetBool(s) 3813 } 3814 } 3815 return clause[j] 3816 } 3817 } 3818 3819 // slice expression: array[low:high:max]. 3820 func slice(n *node) { 3821 i := n.findex 3822 l := n.level 3823 next := getExec(n.tnext) 3824 value0 := genValueArray(n.child[0]) // array 3825 value1 := genValue(n.child[1]) // low (if 2 or 3 args) or high (if 1 arg) 3826 3827 switch len(n.child) { 3828 case 2: 3829 n.exec = func(f *frame) bltn { 3830 a := value0(f) 3831 getFrame(f, l).data[i] = a.Slice(int(vInt(value1(f))), a.Len()) 3832 return next 3833 } 3834 case 3: 3835 value2 := genValue(n.child[2]) // max 3836 3837 n.exec = func(f *frame) bltn { 3838 a := value0(f) 3839 getFrame(f, l).data[i] = a.Slice(int(vInt(value1(f))), int(vInt(value2(f)))) 3840 return next 3841 } 3842 case 4: 3843 value2 := genValue(n.child[2]) 3844 value3 := genValue(n.child[3]) 3845 3846 n.exec = func(f *frame) bltn { 3847 a := value0(f) 3848 getFrame(f, l).data[i] = a.Slice3(int(vInt(value1(f))), int(vInt(value2(f))), int(vInt(value3(f)))) 3849 return next 3850 } 3851 } 3852 } 3853 3854 // slice expression, no low value: array[:high:max]. 3855 func slice0(n *node) { 3856 i := n.findex 3857 l := n.level 3858 next := getExec(n.tnext) 3859 value0 := genValueArray(n.child[0]) 3860 3861 switch len(n.child) { 3862 case 1: 3863 n.exec = func(f *frame) bltn { 3864 a := value0(f) 3865 getFrame(f, l).data[i] = a.Slice(0, a.Len()) 3866 return next 3867 } 3868 case 2: 3869 value1 := genValue(n.child[1]) 3870 n.exec = func(f *frame) bltn { 3871 a := value0(f) 3872 getFrame(f, l).data[i] = a.Slice(0, int(vInt(value1(f)))) 3873 return next 3874 } 3875 case 3: 3876 value1 := genValue(n.child[1]) 3877 value2 := genValue(n.child[2]) 3878 n.exec = func(f *frame) bltn { 3879 a := value0(f) 3880 getFrame(f, l).data[i] = a.Slice3(0, int(vInt(value1(f))), int(vInt(value2(f)))) 3881 return next 3882 } 3883 } 3884 } 3885 3886 func isNilChild(child int) func(n *node) { 3887 return func(n *node) { 3888 var value func(*frame) reflect.Value 3889 child := n.child[child] 3890 value = genValue(child) 3891 typ := n.typ.concrete().TypeOf() 3892 isInterface := n.typ.TypeOf().Kind() == reflect.Interface 3893 tnext := getExec(n.tnext) 3894 dest := genValue(n) 3895 if n.fnext == nil { 3896 if !isInterfaceSrc(child.typ) { 3897 if isInterface { 3898 n.exec = func(f *frame) bltn { 3899 dest(f).Set(reflect.ValueOf(value(f).IsNil()).Convert(typ)) 3900 return tnext 3901 } 3902 return 3903 } 3904 n.exec = func(f *frame) bltn { 3905 dest(f).SetBool(value(f).IsNil()) 3906 return tnext 3907 } 3908 return 3909 } 3910 if isInterface { 3911 n.exec = func(f *frame) bltn { 3912 v := value(f) 3913 var r bool 3914 if vi, ok := v.Interface().(valueInterface); ok { 3915 r = (vi == valueInterface{} || vi.node.kind == basicLit && vi.node.typ.cat == nilT) 3916 } else { 3917 r = v.IsNil() 3918 } 3919 dest(f).Set(reflect.ValueOf(r).Convert(typ)) 3920 return tnext 3921 } 3922 return 3923 } 3924 n.exec = func(f *frame) bltn { 3925 v := value(f) 3926 var r bool 3927 if vi, ok := v.Interface().(valueInterface); ok { 3928 r = (vi == valueInterface{} || vi.node.kind == basicLit && vi.node.typ.cat == nilT) 3929 } else { 3930 r = v.IsNil() 3931 } 3932 dest(f).SetBool(r) 3933 return tnext 3934 } 3935 return 3936 } 3937 3938 fnext := getExec(n.fnext) 3939 3940 if !isInterfaceSrc(child.typ) { 3941 n.exec = func(f *frame) bltn { 3942 if value(f).IsNil() { 3943 dest(f).SetBool(true) 3944 return tnext 3945 } 3946 dest(f).SetBool(false) 3947 return fnext 3948 } 3949 return 3950 } 3951 3952 n.exec = func(f *frame) bltn { 3953 v := value(f) 3954 if vi, ok := v.Interface().(valueInterface); ok { 3955 if (vi == valueInterface{} || vi.node.kind == basicLit && vi.node.typ.cat == nilT) { 3956 dest(f).SetBool(true) 3957 return tnext 3958 } 3959 dest(f).SetBool(false) 3960 return fnext 3961 } 3962 if v.IsNil() { 3963 dest(f).SetBool(true) 3964 return tnext 3965 } 3966 dest(f).SetBool(false) 3967 return fnext 3968 } 3969 } 3970 } 3971 3972 func isNotNil(n *node) { 3973 var value func(*frame) reflect.Value 3974 c0 := n.child[0] 3975 value = genValue(c0) 3976 typ := n.typ.concrete().TypeOf() 3977 isInterface := n.typ.TypeOf().Kind() == reflect.Interface 3978 tnext := getExec(n.tnext) 3979 dest := genValue(n) 3980 3981 if n.fnext == nil { 3982 if isInterfaceSrc(c0.typ) && c0.typ.TypeOf() != valueInterfaceType { 3983 if isInterface { 3984 n.exec = func(f *frame) bltn { 3985 dest(f).Set(reflect.ValueOf(!value(f).IsNil()).Convert(typ)) 3986 return tnext 3987 } 3988 return 3989 } 3990 n.exec = func(f *frame) bltn { 3991 dest(f).SetBool(!value(f).IsNil()) 3992 return tnext 3993 } 3994 return 3995 } 3996 3997 if isInterface { 3998 n.exec = func(f *frame) bltn { 3999 v := value(f) 4000 var r bool 4001 if vi, ok := v.Interface().(valueInterface); ok { 4002 r = (vi == valueInterface{} || vi.node.kind == basicLit && vi.node.typ.cat == nilT) 4003 } else { 4004 r = v.IsNil() 4005 } 4006 dest(f).Set(reflect.ValueOf(!r).Convert(typ)) 4007 return tnext 4008 } 4009 return 4010 } 4011 n.exec = func(f *frame) bltn { 4012 v := value(f) 4013 var r bool 4014 if vi, ok := v.Interface().(valueInterface); ok { 4015 r = (vi == valueInterface{} || vi.node.kind == basicLit && vi.node.typ.cat == nilT) 4016 } else { 4017 r = v.IsNil() 4018 } 4019 dest(f).SetBool(!r) 4020 return tnext 4021 } 4022 return 4023 } 4024 4025 fnext := getExec(n.fnext) 4026 4027 if isInterfaceSrc(c0.typ) && c0.typ.TypeOf() != valueInterfaceType { 4028 n.exec = func(f *frame) bltn { 4029 if value(f).IsNil() { 4030 dest(f).SetBool(false) 4031 return fnext 4032 } 4033 dest(f).SetBool(true) 4034 return tnext 4035 } 4036 return 4037 } 4038 4039 n.exec = func(f *frame) bltn { 4040 v := value(f) 4041 if vi, ok := v.Interface().(valueInterface); ok { 4042 if (vi == valueInterface{} || vi.node.kind == basicLit && vi.node.typ.cat == nilT) { 4043 dest(f).SetBool(false) 4044 return fnext 4045 } 4046 dest(f).SetBool(true) 4047 return tnext 4048 } 4049 if v.IsNil() { 4050 dest(f).SetBool(false) 4051 return fnext 4052 } 4053 dest(f).SetBool(true) 4054 return tnext 4055 } 4056 } 4057 4058 func complexConst(n *node) { 4059 if v0, v1 := n.child[1].rval, n.child[2].rval; v0.IsValid() && v1.IsValid() { 4060 n.rval = reflect.ValueOf(complex(vFloat(v0), vFloat(v1))) 4061 n.gen = nop 4062 } 4063 } 4064 4065 func imagConst(n *node) { 4066 if v := n.child[1].rval; v.IsValid() { 4067 n.rval = reflect.ValueOf(imag(v.Complex())) 4068 n.gen = nop 4069 } 4070 } 4071 4072 func realConst(n *node) { 4073 if v := n.child[1].rval; v.IsValid() { 4074 n.rval = reflect.ValueOf(real(v.Complex())) 4075 n.gen = nop 4076 } 4077 }