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