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