github.com/muesli/go@v0.0.0-20170208044820-e410d2a81ef2/src/cmd/compile/internal/gc/subr.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gc
     6  
     7  import (
     8  	"bytes"
     9  	"cmd/internal/obj"
    10  	"cmd/internal/src"
    11  	"crypto/md5"
    12  	"encoding/binary"
    13  	"fmt"
    14  	"os"
    15  	"runtime/debug"
    16  	"sort"
    17  	"strconv"
    18  	"strings"
    19  	"unicode"
    20  	"unicode/utf8"
    21  )
    22  
    23  type Error struct {
    24  	pos src.XPos
    25  	msg string
    26  }
    27  
    28  var errors []Error
    29  
    30  func errorexit() {
    31  	flusherrors()
    32  	if outfile != "" {
    33  		os.Remove(outfile)
    34  	}
    35  	os.Exit(2)
    36  }
    37  
    38  func adderrorname(n *Node) {
    39  	if n.Op != ODOT {
    40  		return
    41  	}
    42  	old := fmt.Sprintf("%v: undefined: %v\n", n.Line(), n.Left)
    43  	if len(errors) > 0 && errors[len(errors)-1].pos.Line() == n.Pos.Line() && errors[len(errors)-1].msg == old {
    44  		errors[len(errors)-1].msg = fmt.Sprintf("%v: undefined: %v in %v\n", n.Line(), n.Left, n)
    45  	}
    46  }
    47  
    48  func adderr(pos src.XPos, format string, args ...interface{}) {
    49  	errors = append(errors, Error{
    50  		pos: pos,
    51  		msg: fmt.Sprintf("%v: %s\n", linestr(pos), fmt.Sprintf(format, args...)),
    52  	})
    53  }
    54  
    55  // byPos sorts errors by source position.
    56  type byPos []Error
    57  
    58  func (x byPos) Len() int           { return len(x) }
    59  func (x byPos) Less(i, j int) bool { return x[i].pos.Before(x[j].pos) }
    60  func (x byPos) Swap(i, j int)      { x[i], x[j] = x[j], x[i] }
    61  
    62  // flusherrors sorts errors seen so far by line number, prints them to stdout,
    63  // and empties the errors array.
    64  func flusherrors() {
    65  	Ctxt.Bso.Flush()
    66  	if len(errors) == 0 {
    67  		return
    68  	}
    69  	sort.Stable(byPos(errors))
    70  	for i := 0; i < len(errors); i++ {
    71  		if i == 0 || errors[i].msg != errors[i-1].msg {
    72  			fmt.Printf("%s", errors[i].msg)
    73  		}
    74  	}
    75  	errors = errors[:0]
    76  }
    77  
    78  func hcrash() {
    79  	if Debug['h'] != 0 {
    80  		flusherrors()
    81  		if outfile != "" {
    82  			os.Remove(outfile)
    83  		}
    84  		var x *int
    85  		*x = 0
    86  	}
    87  }
    88  
    89  func linestr(pos src.XPos) string {
    90  	return Ctxt.PosTable.Pos(pos).String()
    91  }
    92  
    93  // lasterror keeps track of the most recently issued error.
    94  // It is used to avoid multiple error messages on the same
    95  // line.
    96  var lasterror struct {
    97  	syntax src.XPos // source position of last syntax error
    98  	other  src.XPos // source position of last non-syntax error
    99  	msg    string   // error message of last non-syntax error
   100  }
   101  
   102  // sameline reports whether two positions a, b are on the same line.
   103  func sameline(a, b src.XPos) bool {
   104  	p := Ctxt.PosTable.Pos(a)
   105  	q := Ctxt.PosTable.Pos(b)
   106  	return p.Base() == q.Base() && p.Line() == q.Line()
   107  }
   108  
   109  func yyerrorl(pos src.XPos, format string, args ...interface{}) {
   110  	msg := fmt.Sprintf(format, args...)
   111  
   112  	if strings.HasPrefix(msg, "syntax error") {
   113  		nsyntaxerrors++
   114  		// only one syntax error per line, no matter what error
   115  		if sameline(lasterror.syntax, pos) {
   116  			return
   117  		}
   118  		lasterror.syntax = pos
   119  	} else {
   120  		// only one of multiple equal non-syntax errors per line
   121  		// (flusherrors shows only one of them, so we filter them
   122  		// here as best as we can (they may not appear in order)
   123  		// so that we don't count them here and exit early, and
   124  		// then have nothing to show for.)
   125  		if sameline(lasterror.other, pos) && lasterror.msg == msg {
   126  			return
   127  		}
   128  		lasterror.other = pos
   129  		lasterror.msg = msg
   130  	}
   131  
   132  	adderr(pos, "%s", msg)
   133  
   134  	hcrash()
   135  	nerrors++
   136  	if nsavederrors+nerrors >= 10 && Debug['e'] == 0 {
   137  		flusherrors()
   138  		fmt.Printf("%v: too many errors\n", linestr(pos))
   139  		errorexit()
   140  	}
   141  }
   142  
   143  func yyerror(format string, args ...interface{}) {
   144  	yyerrorl(lineno, format, args...)
   145  }
   146  
   147  func Warn(fmt_ string, args ...interface{}) {
   148  	adderr(lineno, fmt_, args...)
   149  
   150  	hcrash()
   151  }
   152  
   153  func Warnl(line src.XPos, fmt_ string, args ...interface{}) {
   154  	adderr(line, fmt_, args...)
   155  	if Debug['m'] != 0 {
   156  		flusherrors()
   157  	}
   158  }
   159  
   160  func Fatalf(fmt_ string, args ...interface{}) {
   161  	flusherrors()
   162  
   163  	fmt.Printf("%v: internal compiler error: ", linestr(lineno))
   164  	fmt.Printf(fmt_, args...)
   165  	fmt.Printf("\n")
   166  
   167  	// If this is a released compiler version, ask for a bug report.
   168  	if strings.HasPrefix(obj.Version, "release") {
   169  		fmt.Printf("\n")
   170  		fmt.Printf("Please file a bug report including a short program that triggers the error.\n")
   171  		fmt.Printf("https://golang.org/issue/new\n")
   172  	} else {
   173  		// Not a release; dump a stack trace, too.
   174  		fmt.Println()
   175  		os.Stdout.Write(debug.Stack())
   176  		fmt.Println()
   177  	}
   178  
   179  	hcrash()
   180  	errorexit()
   181  }
   182  
   183  func setlineno(n *Node) src.XPos {
   184  	lno := lineno
   185  	if n != nil {
   186  		switch n.Op {
   187  		case ONAME, OPACK:
   188  			break
   189  
   190  		case OLITERAL, OTYPE:
   191  			if n.Sym != nil {
   192  				break
   193  			}
   194  			fallthrough
   195  
   196  		default:
   197  			lineno = n.Pos
   198  			if !lineno.IsKnown() {
   199  				if Debug['K'] != 0 {
   200  					Warn("setlineno: unknown position (line 0)")
   201  				}
   202  				lineno = lno
   203  			}
   204  		}
   205  	}
   206  
   207  	return lno
   208  }
   209  
   210  func lookup(name string) *Sym {
   211  	return localpkg.Lookup(name)
   212  }
   213  
   214  func lookupf(format string, a ...interface{}) *Sym {
   215  	return lookup(fmt.Sprintf(format, a...))
   216  }
   217  
   218  func lookupBytes(name []byte) *Sym {
   219  	return localpkg.LookupBytes(name)
   220  }
   221  
   222  // lookupN looks up the symbol starting with prefix and ending with
   223  // the decimal n. If prefix is too long, lookupN panics.
   224  func lookupN(prefix string, n int) *Sym {
   225  	var buf [20]byte // plenty long enough for all current users
   226  	copy(buf[:], prefix)
   227  	b := strconv.AppendInt(buf[:len(prefix)], int64(n), 10)
   228  	return lookupBytes(b)
   229  }
   230  
   231  // autolabel generates a new Name node for use with
   232  // an automatically generated label.
   233  // prefix is a short mnemonic (e.g. ".s" for switch)
   234  // to help with debugging.
   235  // It should begin with "." to avoid conflicts with
   236  // user labels.
   237  func autolabel(prefix string) *Node {
   238  	if prefix[0] != '.' {
   239  		Fatalf("autolabel prefix must start with '.', have %q", prefix)
   240  	}
   241  	fn := Curfn
   242  	if Curfn == nil {
   243  		Fatalf("autolabel outside function")
   244  	}
   245  	n := fn.Func.Label
   246  	fn.Func.Label++
   247  	return newname(lookupN(prefix, int(n)))
   248  }
   249  
   250  var initSyms []*Sym
   251  
   252  var nopkg = &Pkg{
   253  	Syms: make(map[string]*Sym),
   254  }
   255  
   256  func (pkg *Pkg) Lookup(name string) *Sym {
   257  	if pkg == nil {
   258  		pkg = nopkg
   259  	}
   260  	if s := pkg.Syms[name]; s != nil {
   261  		return s
   262  	}
   263  
   264  	s := &Sym{
   265  		Name: name,
   266  		Pkg:  pkg,
   267  	}
   268  	if name == "init" {
   269  		initSyms = append(initSyms, s)
   270  	}
   271  	pkg.Syms[name] = s
   272  	return s
   273  }
   274  
   275  func (pkg *Pkg) LookupBytes(name []byte) *Sym {
   276  	if pkg == nil {
   277  		pkg = nopkg
   278  	}
   279  	if s := pkg.Syms[string(name)]; s != nil {
   280  		return s
   281  	}
   282  	str := internString(name)
   283  	return pkg.Lookup(str)
   284  }
   285  
   286  func Pkglookup(name string, pkg *Pkg) *Sym {
   287  	return pkg.Lookup(name)
   288  }
   289  
   290  func restrictlookup(name string, pkg *Pkg) *Sym {
   291  	if !exportname(name) && pkg != localpkg {
   292  		yyerror("cannot refer to unexported name %s.%s", pkg.Name, name)
   293  	}
   294  	return Pkglookup(name, pkg)
   295  }
   296  
   297  // find all the exported symbols in package opkg
   298  // and make them available in the current package
   299  func importdot(opkg *Pkg, pack *Node) {
   300  	var s1 *Sym
   301  	var pkgerror string
   302  
   303  	n := 0
   304  	for _, s := range opkg.Syms {
   305  		if s.Def == nil {
   306  			continue
   307  		}
   308  		if !exportname(s.Name) || strings.ContainsRune(s.Name, 0xb7) { // 0xb7 = center dot
   309  			continue
   310  		}
   311  		s1 = lookup(s.Name)
   312  		if s1.Def != nil {
   313  			pkgerror = fmt.Sprintf("during import %q", opkg.Path)
   314  			redeclare(s1, pkgerror)
   315  			continue
   316  		}
   317  
   318  		s1.Def = s.Def
   319  		s1.Block = s.Block
   320  		if s1.Def.Name == nil {
   321  			Dump("s1def", s1.Def)
   322  			Fatalf("missing Name")
   323  		}
   324  		s1.Def.Name.Pack = pack
   325  		s1.Origpkg = opkg
   326  		n++
   327  	}
   328  
   329  	if n == 0 {
   330  		// can't possibly be used - there were no symbols
   331  		yyerrorl(pack.Pos, "imported and not used: %q", opkg.Path)
   332  	}
   333  }
   334  
   335  func nod(op Op, nleft *Node, nright *Node) *Node {
   336  	var n *Node
   337  	switch op {
   338  	case OCLOSURE, ODCLFUNC:
   339  		var x struct {
   340  			Node
   341  			Func
   342  		}
   343  		n = &x.Node
   344  		n.Func = &x.Func
   345  		n.Func.IsHiddenClosure = Curfn != nil
   346  	case ONAME:
   347  		var x struct {
   348  			Node
   349  			Name
   350  			Param
   351  		}
   352  		n = &x.Node
   353  		n.Name = &x.Name
   354  		n.Name.Param = &x.Param
   355  	case OLABEL, OPACK:
   356  		var x struct {
   357  			Node
   358  			Name
   359  		}
   360  		n = &x.Node
   361  		n.Name = &x.Name
   362  	default:
   363  		n = new(Node)
   364  	}
   365  	n.Op = op
   366  	n.Left = nleft
   367  	n.Right = nright
   368  	n.Pos = lineno
   369  	n.Xoffset = BADWIDTH
   370  	n.Orig = n
   371  	if n.Name != nil {
   372  		n.Name.Curfn = Curfn
   373  	}
   374  	return n
   375  }
   376  
   377  // nodSym makes a Node with Op op and with the Left field set to left
   378  // and the Sym field set to sym. This is for ODOT and friends.
   379  func nodSym(op Op, left *Node, sym *Sym) *Node {
   380  	n := nod(op, left, nil)
   381  	n.Sym = sym
   382  	return n
   383  }
   384  
   385  func saveorignode(n *Node) {
   386  	if n.Orig != nil {
   387  		return
   388  	}
   389  	norig := nod(n.Op, nil, nil)
   390  	*norig = *n
   391  	n.Orig = norig
   392  }
   393  
   394  // methcmp sorts by symbol, then by package path for unexported symbols.
   395  type methcmp []*Field
   396  
   397  func (x methcmp) Len() int      { return len(x) }
   398  func (x methcmp) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
   399  func (x methcmp) Less(i, j int) bool {
   400  	a := x[i]
   401  	b := x[j]
   402  	if a.Sym == nil && b.Sym == nil {
   403  		return false
   404  	}
   405  	if a.Sym == nil {
   406  		return true
   407  	}
   408  	if b.Sym == nil {
   409  		return false
   410  	}
   411  	if a.Sym.Name != b.Sym.Name {
   412  		return a.Sym.Name < b.Sym.Name
   413  	}
   414  	if !exportname(a.Sym.Name) {
   415  		if a.Sym.Pkg.Path != b.Sym.Pkg.Path {
   416  			return a.Sym.Pkg.Path < b.Sym.Pkg.Path
   417  		}
   418  	}
   419  
   420  	return false
   421  }
   422  
   423  func nodintconst(v int64) *Node {
   424  	c := nod(OLITERAL, nil, nil)
   425  	c.Addable = true
   426  	c.SetVal(Val{new(Mpint)})
   427  	c.Val().U.(*Mpint).SetInt64(v)
   428  	c.Type = Types[TIDEAL]
   429  	ullmancalc(c)
   430  	return c
   431  }
   432  
   433  func nodfltconst(v *Mpflt) *Node {
   434  	c := nod(OLITERAL, nil, nil)
   435  	c.Addable = true
   436  	c.SetVal(Val{newMpflt()})
   437  	c.Val().U.(*Mpflt).Set(v)
   438  	c.Type = Types[TIDEAL]
   439  	ullmancalc(c)
   440  	return c
   441  }
   442  
   443  func Nodconst(n *Node, t *Type, v int64) {
   444  	*n = Node{}
   445  	n.Op = OLITERAL
   446  	n.Addable = true
   447  	ullmancalc(n)
   448  	n.SetVal(Val{new(Mpint)})
   449  	n.Val().U.(*Mpint).SetInt64(v)
   450  	n.Type = t
   451  
   452  	if t.IsFloat() {
   453  		Fatalf("nodconst: bad type %v", t)
   454  	}
   455  }
   456  
   457  func nodnil() *Node {
   458  	c := nodintconst(0)
   459  	c.SetVal(Val{new(NilVal)})
   460  	c.Type = Types[TNIL]
   461  	return c
   462  }
   463  
   464  func nodbool(b bool) *Node {
   465  	c := nodintconst(0)
   466  	c.SetVal(Val{b})
   467  	c.Type = idealbool
   468  	return c
   469  }
   470  
   471  // treecopy recursively copies n, with the exception of
   472  // ONAME, OLITERAL, OTYPE, and non-iota ONONAME leaves.
   473  // Copies of iota ONONAME nodes are assigned the current
   474  // value of iota_. If pos.IsKnown(), it sets the source
   475  // position of newly allocated nodes to pos.
   476  func treecopy(n *Node, pos src.XPos) *Node {
   477  	if n == nil {
   478  		return nil
   479  	}
   480  
   481  	switch n.Op {
   482  	default:
   483  		m := *n
   484  		m.Orig = &m
   485  		m.Left = treecopy(n.Left, pos)
   486  		m.Right = treecopy(n.Right, pos)
   487  		m.List.Set(listtreecopy(n.List.Slice(), pos))
   488  		if pos.IsKnown() {
   489  			m.Pos = pos
   490  		}
   491  		if m.Name != nil && n.Op != ODCLFIELD {
   492  			Dump("treecopy", n)
   493  			Fatalf("treecopy Name")
   494  		}
   495  		return &m
   496  
   497  	case OPACK:
   498  		// OPACK nodes are never valid in const value declarations,
   499  		// but allow them like any other declared symbol to avoid
   500  		// crashing (golang.org/issue/11361).
   501  		fallthrough
   502  
   503  	case ONAME, ONONAME, OLITERAL, OTYPE:
   504  		return n
   505  
   506  	}
   507  }
   508  
   509  // isnil reports whether n represents the universal untyped zero value "nil".
   510  func isnil(n *Node) bool {
   511  	// Check n.Orig because constant propagation may produce typed nil constants,
   512  	// which don't exist in the Go spec.
   513  	return Isconst(n.Orig, CTNIL)
   514  }
   515  
   516  func isptrto(t *Type, et EType) bool {
   517  	if t == nil {
   518  		return false
   519  	}
   520  	if !t.IsPtr() {
   521  		return false
   522  	}
   523  	t = t.Elem()
   524  	if t == nil {
   525  		return false
   526  	}
   527  	if t.Etype != et {
   528  		return false
   529  	}
   530  	return true
   531  }
   532  
   533  func isblank(n *Node) bool {
   534  	if n == nil {
   535  		return false
   536  	}
   537  	return isblanksym(n.Sym)
   538  }
   539  
   540  func isblanksym(s *Sym) bool {
   541  	return s != nil && s.Name == "_"
   542  }
   543  
   544  // methtype returns the underlying type, if any,
   545  // that owns methods with receiver parameter t.
   546  // The result is either a named type or an anonymous struct.
   547  func methtype(t *Type) *Type {
   548  	if t == nil {
   549  		return nil
   550  	}
   551  
   552  	// Strip away pointer if it's there.
   553  	if t.IsPtr() {
   554  		if t.Sym != nil {
   555  			return nil
   556  		}
   557  		t = t.Elem()
   558  		if t == nil {
   559  			return nil
   560  		}
   561  	}
   562  
   563  	// Must be a named type or anonymous struct.
   564  	if t.Sym == nil && !t.IsStruct() {
   565  		return nil
   566  	}
   567  
   568  	// Check types.
   569  	if issimple[t.Etype] {
   570  		return t
   571  	}
   572  	switch t.Etype {
   573  	case TARRAY, TCHAN, TFUNC, TMAP, TSLICE, TSTRING, TSTRUCT:
   574  		return t
   575  	}
   576  	return nil
   577  }
   578  
   579  func cplxsubtype(et EType) EType {
   580  	switch et {
   581  	case TCOMPLEX64:
   582  		return TFLOAT32
   583  
   584  	case TCOMPLEX128:
   585  		return TFLOAT64
   586  	}
   587  
   588  	Fatalf("cplxsubtype: %v\n", et)
   589  	return 0
   590  }
   591  
   592  // eqtype reports whether t1 and t2 are identical, following the spec rules.
   593  //
   594  // Any cyclic type must go through a named type, and if one is
   595  // named, it is only identical to the other if they are the same
   596  // pointer (t1 == t2), so there's no chance of chasing cycles
   597  // ad infinitum, so no need for a depth counter.
   598  func eqtype(t1, t2 *Type) bool {
   599  	return eqtype1(t1, t2, true, nil)
   600  }
   601  
   602  // eqtypeIgnoreTags is like eqtype but it ignores struct tags for struct identity.
   603  func eqtypeIgnoreTags(t1, t2 *Type) bool {
   604  	return eqtype1(t1, t2, false, nil)
   605  }
   606  
   607  type typePair struct {
   608  	t1 *Type
   609  	t2 *Type
   610  }
   611  
   612  func eqtype1(t1, t2 *Type, cmpTags bool, assumedEqual map[typePair]struct{}) bool {
   613  	if t1 == t2 {
   614  		return true
   615  	}
   616  	if t1 == nil || t2 == nil || t1.Etype != t2.Etype || t1.Broke || t2.Broke {
   617  		return false
   618  	}
   619  	if t1.Sym != nil || t2.Sym != nil {
   620  		// Special case: we keep byte/uint8 and rune/int32
   621  		// separate for error messages. Treat them as equal.
   622  		switch t1.Etype {
   623  		case TUINT8:
   624  			return (t1 == Types[TUINT8] || t1 == bytetype) && (t2 == Types[TUINT8] || t2 == bytetype)
   625  		case TINT32:
   626  			return (t1 == Types[TINT32] || t1 == runetype) && (t2 == Types[TINT32] || t2 == runetype)
   627  		default:
   628  			return false
   629  		}
   630  	}
   631  
   632  	if assumedEqual == nil {
   633  		assumedEqual = make(map[typePair]struct{})
   634  	} else if _, ok := assumedEqual[typePair{t1, t2}]; ok {
   635  		return true
   636  	}
   637  	assumedEqual[typePair{t1, t2}] = struct{}{}
   638  
   639  	switch t1.Etype {
   640  	case TINTER, TSTRUCT:
   641  		t1, i1 := iterFields(t1)
   642  		t2, i2 := iterFields(t2)
   643  		for ; t1 != nil && t2 != nil; t1, t2 = i1.Next(), i2.Next() {
   644  			if t1.Sym != t2.Sym || t1.Embedded != t2.Embedded || !eqtype1(t1.Type, t2.Type, cmpTags, assumedEqual) || cmpTags && t1.Note != t2.Note {
   645  				return false
   646  			}
   647  		}
   648  
   649  		if t1 == nil && t2 == nil {
   650  			return true
   651  		}
   652  		return false
   653  
   654  	case TFUNC:
   655  		// Check parameters and result parameters for type equality.
   656  		// We intentionally ignore receiver parameters for type
   657  		// equality, because they're never relevant.
   658  		for _, f := range paramsResults {
   659  			// Loop over fields in structs, ignoring argument names.
   660  			ta, ia := iterFields(f(t1))
   661  			tb, ib := iterFields(f(t2))
   662  			for ; ta != nil && tb != nil; ta, tb = ia.Next(), ib.Next() {
   663  				if ta.Isddd != tb.Isddd || !eqtype1(ta.Type, tb.Type, cmpTags, assumedEqual) {
   664  					return false
   665  				}
   666  			}
   667  			if ta != nil || tb != nil {
   668  				return false
   669  			}
   670  		}
   671  		return true
   672  
   673  	case TARRAY:
   674  		if t1.NumElem() != t2.NumElem() {
   675  			return false
   676  		}
   677  
   678  	case TCHAN:
   679  		if t1.ChanDir() != t2.ChanDir() {
   680  			return false
   681  		}
   682  
   683  	case TMAP:
   684  		if !eqtype1(t1.Key(), t2.Key(), cmpTags, assumedEqual) {
   685  			return false
   686  		}
   687  		return eqtype1(t1.Val(), t2.Val(), cmpTags, assumedEqual)
   688  	}
   689  
   690  	return eqtype1(t1.Elem(), t2.Elem(), cmpTags, assumedEqual)
   691  }
   692  
   693  // Are t1 and t2 equal struct types when field names are ignored?
   694  // For deciding whether the result struct from g can be copied
   695  // directly when compiling f(g()).
   696  func eqtypenoname(t1 *Type, t2 *Type) bool {
   697  	if t1 == nil || t2 == nil || !t1.IsStruct() || !t2.IsStruct() {
   698  		return false
   699  	}
   700  
   701  	f1, i1 := iterFields(t1)
   702  	f2, i2 := iterFields(t2)
   703  	for {
   704  		if !eqtype(f1.Type, f2.Type) {
   705  			return false
   706  		}
   707  		if f1 == nil {
   708  			return true
   709  		}
   710  		f1 = i1.Next()
   711  		f2 = i2.Next()
   712  	}
   713  }
   714  
   715  // Is type src assignment compatible to type dst?
   716  // If so, return op code to use in conversion.
   717  // If not, return 0.
   718  func assignop(src *Type, dst *Type, why *string) Op {
   719  	if why != nil {
   720  		*why = ""
   721  	}
   722  
   723  	// TODO(rsc,lvd): This behaves poorly in the presence of inlining.
   724  	// https://golang.org/issue/2795
   725  	if safemode && importpkg == nil && src != nil && src.Etype == TUNSAFEPTR {
   726  		yyerror("cannot use unsafe.Pointer")
   727  		errorexit()
   728  	}
   729  
   730  	if src == dst {
   731  		return OCONVNOP
   732  	}
   733  	if src == nil || dst == nil || src.Etype == TFORW || dst.Etype == TFORW || src.Orig == nil || dst.Orig == nil {
   734  		return 0
   735  	}
   736  
   737  	// 1. src type is identical to dst.
   738  	if eqtype(src, dst) {
   739  		return OCONVNOP
   740  	}
   741  
   742  	// 2. src and dst have identical underlying types
   743  	// and either src or dst is not a named type or
   744  	// both are empty interface types.
   745  	// For assignable but different non-empty interface types,
   746  	// we want to recompute the itab. Recomputing the itab ensures
   747  	// that itabs are unique (thus an interface with a compile-time
   748  	// type I has an itab with interface type I).
   749  	if eqtype(src.Orig, dst.Orig) {
   750  		if src.IsEmptyInterface() {
   751  			// Conversion between two empty interfaces
   752  			// requires no code.
   753  			return OCONVNOP
   754  		}
   755  		if (src.Sym == nil || dst.Sym == nil) && !src.IsInterface() {
   756  			// Conversion between two types, at least one unnamed,
   757  			// needs no conversion. The exception is nonempty interfaces
   758  			// which need to have their itab updated.
   759  			return OCONVNOP
   760  		}
   761  	}
   762  
   763  	// 3. dst is an interface type and src implements dst.
   764  	if dst.IsInterface() && src.Etype != TNIL {
   765  		var missing, have *Field
   766  		var ptr int
   767  		if implements(src, dst, &missing, &have, &ptr) {
   768  			return OCONVIFACE
   769  		}
   770  
   771  		// we'll have complained about this method anyway, suppress spurious messages.
   772  		if have != nil && have.Sym == missing.Sym && (have.Type.Broke || missing.Type.Broke) {
   773  			return OCONVIFACE
   774  		}
   775  
   776  		if why != nil {
   777  			if isptrto(src, TINTER) {
   778  				*why = fmt.Sprintf(":\n\t%v is pointer to interface, not interface", src)
   779  			} else if have != nil && have.Sym == missing.Sym && have.Nointerface {
   780  				*why = fmt.Sprintf(":\n\t%v does not implement %v (%v method is marked 'nointerface')", src, dst, missing.Sym)
   781  			} else if have != nil && have.Sym == missing.Sym {
   782  				*why = fmt.Sprintf(":\n\t%v does not implement %v (wrong type for %v method)\n"+
   783  					"\t\thave %v%0S\n\t\twant %v%0S", src, dst, missing.Sym, have.Sym, have.Type, missing.Sym, missing.Type)
   784  			} else if ptr != 0 {
   785  				*why = fmt.Sprintf(":\n\t%v does not implement %v (%v method has pointer receiver)", src, dst, missing.Sym)
   786  			} else if have != nil {
   787  				*why = fmt.Sprintf(":\n\t%v does not implement %v (missing %v method)\n"+
   788  					"\t\thave %v%0S\n\t\twant %v%0S", src, dst, missing.Sym, have.Sym, have.Type, missing.Sym, missing.Type)
   789  			} else {
   790  				*why = fmt.Sprintf(":\n\t%v does not implement %v (missing %v method)", src, dst, missing.Sym)
   791  			}
   792  		}
   793  
   794  		return 0
   795  	}
   796  
   797  	if isptrto(dst, TINTER) {
   798  		if why != nil {
   799  			*why = fmt.Sprintf(":\n\t%v is pointer to interface, not interface", dst)
   800  		}
   801  		return 0
   802  	}
   803  
   804  	if src.IsInterface() && dst.Etype != TBLANK {
   805  		var missing, have *Field
   806  		var ptr int
   807  		if why != nil && implements(dst, src, &missing, &have, &ptr) {
   808  			*why = ": need type assertion"
   809  		}
   810  		return 0
   811  	}
   812  
   813  	// 4. src is a bidirectional channel value, dst is a channel type,
   814  	// src and dst have identical element types, and
   815  	// either src or dst is not a named type.
   816  	if src.IsChan() && src.ChanDir() == Cboth && dst.IsChan() {
   817  		if eqtype(src.Elem(), dst.Elem()) && (src.Sym == nil || dst.Sym == nil) {
   818  			return OCONVNOP
   819  		}
   820  	}
   821  
   822  	// 5. src is the predeclared identifier nil and dst is a nillable type.
   823  	if src.Etype == TNIL {
   824  		switch dst.Etype {
   825  		case TPTR32,
   826  			TPTR64,
   827  			TFUNC,
   828  			TMAP,
   829  			TCHAN,
   830  			TINTER,
   831  			TSLICE:
   832  			return OCONVNOP
   833  		}
   834  	}
   835  
   836  	// 6. rule about untyped constants - already converted by defaultlit.
   837  
   838  	// 7. Any typed value can be assigned to the blank identifier.
   839  	if dst.Etype == TBLANK {
   840  		return OCONVNOP
   841  	}
   842  
   843  	return 0
   844  }
   845  
   846  // Can we convert a value of type src to a value of type dst?
   847  // If so, return op code to use in conversion (maybe OCONVNOP).
   848  // If not, return 0.
   849  func convertop(src *Type, dst *Type, why *string) Op {
   850  	if why != nil {
   851  		*why = ""
   852  	}
   853  
   854  	if src == dst {
   855  		return OCONVNOP
   856  	}
   857  	if src == nil || dst == nil {
   858  		return 0
   859  	}
   860  
   861  	// Conversions from regular to go:notinheap are not allowed
   862  	// (unless it's unsafe.Pointer). This is a runtime-specific
   863  	// rule.
   864  	if src.IsPtr() && dst.IsPtr() && dst.Elem().NotInHeap && !src.Elem().NotInHeap {
   865  		if why != nil {
   866  			*why = fmt.Sprintf(":\n\t%v is go:notinheap, but %v is not", dst.Elem(), src.Elem())
   867  		}
   868  		return 0
   869  	}
   870  
   871  	// 1. src can be assigned to dst.
   872  	op := assignop(src, dst, why)
   873  	if op != 0 {
   874  		return op
   875  	}
   876  
   877  	// The rules for interfaces are no different in conversions
   878  	// than assignments. If interfaces are involved, stop now
   879  	// with the good message from assignop.
   880  	// Otherwise clear the error.
   881  	if src.IsInterface() || dst.IsInterface() {
   882  		return 0
   883  	}
   884  	if why != nil {
   885  		*why = ""
   886  	}
   887  
   888  	// 2. Ignoring struct tags, src and dst have identical underlying types.
   889  	if eqtypeIgnoreTags(src.Orig, dst.Orig) {
   890  		return OCONVNOP
   891  	}
   892  
   893  	// 3. src and dst are unnamed pointer types and, ignoring struct tags,
   894  	// their base types have identical underlying types.
   895  	if src.IsPtr() && dst.IsPtr() && src.Sym == nil && dst.Sym == nil {
   896  		if eqtypeIgnoreTags(src.Elem().Orig, dst.Elem().Orig) {
   897  			return OCONVNOP
   898  		}
   899  	}
   900  
   901  	// 4. src and dst are both integer or floating point types.
   902  	if (src.IsInteger() || src.IsFloat()) && (dst.IsInteger() || dst.IsFloat()) {
   903  		if simtype[src.Etype] == simtype[dst.Etype] {
   904  			return OCONVNOP
   905  		}
   906  		return OCONV
   907  	}
   908  
   909  	// 5. src and dst are both complex types.
   910  	if src.IsComplex() && dst.IsComplex() {
   911  		if simtype[src.Etype] == simtype[dst.Etype] {
   912  			return OCONVNOP
   913  		}
   914  		return OCONV
   915  	}
   916  
   917  	// 6. src is an integer or has type []byte or []rune
   918  	// and dst is a string type.
   919  	if src.IsInteger() && dst.IsString() {
   920  		return ORUNESTR
   921  	}
   922  
   923  	if src.IsSlice() && dst.IsString() {
   924  		if src.Elem().Etype == bytetype.Etype {
   925  			return OARRAYBYTESTR
   926  		}
   927  		if src.Elem().Etype == runetype.Etype {
   928  			return OARRAYRUNESTR
   929  		}
   930  	}
   931  
   932  	// 7. src is a string and dst is []byte or []rune.
   933  	// String to slice.
   934  	if src.IsString() && dst.IsSlice() {
   935  		if dst.Elem().Etype == bytetype.Etype {
   936  			return OSTRARRAYBYTE
   937  		}
   938  		if dst.Elem().Etype == runetype.Etype {
   939  			return OSTRARRAYRUNE
   940  		}
   941  	}
   942  
   943  	// 8. src is a pointer or uintptr and dst is unsafe.Pointer.
   944  	if (src.IsPtr() || src.Etype == TUINTPTR) && dst.Etype == TUNSAFEPTR {
   945  		return OCONVNOP
   946  	}
   947  
   948  	// 9. src is unsafe.Pointer and dst is a pointer or uintptr.
   949  	if src.Etype == TUNSAFEPTR && (dst.IsPtr() || dst.Etype == TUINTPTR) {
   950  		return OCONVNOP
   951  	}
   952  
   953  	return 0
   954  }
   955  
   956  func assignconv(n *Node, t *Type, context string) *Node {
   957  	return assignconvfn(n, t, func() string { return context })
   958  }
   959  
   960  // Convert node n for assignment to type t.
   961  func assignconvfn(n *Node, t *Type, context func() string) *Node {
   962  	if n == nil || n.Type == nil || n.Type.Broke {
   963  		return n
   964  	}
   965  
   966  	if t.Etype == TBLANK && n.Type.Etype == TNIL {
   967  		yyerror("use of untyped nil")
   968  	}
   969  
   970  	old := n
   971  	od := old.Diag
   972  	old.Diag = true // silence errors about n; we'll issue one below
   973  	n = defaultlit(n, t)
   974  	old.Diag = od
   975  	if t.Etype == TBLANK {
   976  		return n
   977  	}
   978  
   979  	// Convert ideal bool from comparison to plain bool
   980  	// if the next step is non-bool (like interface{}).
   981  	if n.Type == idealbool && !t.IsBoolean() {
   982  		if n.Op == ONAME || n.Op == OLITERAL {
   983  			r := nod(OCONVNOP, n, nil)
   984  			r.Type = Types[TBOOL]
   985  			r.Typecheck = 1
   986  			r.Implicit = true
   987  			n = r
   988  		}
   989  	}
   990  
   991  	if eqtype(n.Type, t) {
   992  		return n
   993  	}
   994  
   995  	var why string
   996  	op := assignop(n.Type, t, &why)
   997  	if op == 0 {
   998  		yyerror("cannot use %L as type %v in %s%s", n, t, context(), why)
   999  		op = OCONV
  1000  	}
  1001  
  1002  	r := nod(op, n, nil)
  1003  	r.Type = t
  1004  	r.Typecheck = 1
  1005  	r.Implicit = true
  1006  	r.Orig = n.Orig
  1007  	return r
  1008  }
  1009  
  1010  // IsMethod reports whether n is a method.
  1011  // n must be a function or a method.
  1012  func (n *Node) IsMethod() bool {
  1013  	return n.Type.Recv() != nil
  1014  }
  1015  
  1016  // SliceBounds returns n's slice bounds: low, high, and max in expr[low:high:max].
  1017  // n must be a slice expression. max is nil if n is a simple slice expression.
  1018  func (n *Node) SliceBounds() (low, high, max *Node) {
  1019  	if n.List.Len() == 0 {
  1020  		return nil, nil, nil
  1021  	}
  1022  
  1023  	switch n.Op {
  1024  	case OSLICE, OSLICEARR, OSLICESTR:
  1025  		s := n.List.Slice()
  1026  		return s[0], s[1], nil
  1027  	case OSLICE3, OSLICE3ARR:
  1028  		s := n.List.Slice()
  1029  		return s[0], s[1], s[2]
  1030  	}
  1031  	Fatalf("SliceBounds op %v: %v", n.Op, n)
  1032  	return nil, nil, nil
  1033  }
  1034  
  1035  // SetSliceBounds sets n's slice bounds, where n is a slice expression.
  1036  // n must be a slice expression. If max is non-nil, n must be a full slice expression.
  1037  func (n *Node) SetSliceBounds(low, high, max *Node) {
  1038  	switch n.Op {
  1039  	case OSLICE, OSLICEARR, OSLICESTR:
  1040  		if max != nil {
  1041  			Fatalf("SetSliceBounds %v given three bounds", n.Op)
  1042  		}
  1043  		s := n.List.Slice()
  1044  		if s == nil {
  1045  			if low == nil && high == nil {
  1046  				return
  1047  			}
  1048  			n.List.Set([]*Node{low, high})
  1049  			return
  1050  		}
  1051  		s[0] = low
  1052  		s[1] = high
  1053  		return
  1054  	case OSLICE3, OSLICE3ARR:
  1055  		s := n.List.Slice()
  1056  		if s == nil {
  1057  			if low == nil && high == nil && max == nil {
  1058  				return
  1059  			}
  1060  			n.List.Set([]*Node{low, high, max})
  1061  			return
  1062  		}
  1063  		s[0] = low
  1064  		s[1] = high
  1065  		s[2] = max
  1066  		return
  1067  	}
  1068  	Fatalf("SetSliceBounds op %v: %v", n.Op, n)
  1069  }
  1070  
  1071  // IsSlice3 reports whether o is a slice3 op (OSLICE3, OSLICE3ARR).
  1072  // o must be a slicing op.
  1073  func (o Op) IsSlice3() bool {
  1074  	switch o {
  1075  	case OSLICE, OSLICEARR, OSLICESTR:
  1076  		return false
  1077  	case OSLICE3, OSLICE3ARR:
  1078  		return true
  1079  	}
  1080  	Fatalf("IsSlice3 op %v", o)
  1081  	return false
  1082  }
  1083  
  1084  func syslook(name string) *Node {
  1085  	s := Pkglookup(name, Runtimepkg)
  1086  	if s == nil || s.Def == nil {
  1087  		Fatalf("syslook: can't find runtime.%s", name)
  1088  	}
  1089  	return s.Def
  1090  }
  1091  
  1092  // typehash computes a hash value for type t to use in type switch
  1093  // statements.
  1094  func typehash(t *Type) uint32 {
  1095  	// t.tconv(FmtLeft | FmtUnsigned) already contains all the necessary logic
  1096  	// to generate a representation that completely describes the type, so using
  1097  	// it here avoids duplicating that code.
  1098  	// See the comments in exprSwitch.checkDupCases.
  1099  	p := t.tconv(FmtLeft | FmtUnsigned)
  1100  
  1101  	// Using MD5 is overkill, but reduces accidental collisions.
  1102  	h := md5.Sum([]byte(p))
  1103  	return binary.LittleEndian.Uint32(h[:4])
  1104  }
  1105  
  1106  // ptrto returns the Type *t.
  1107  // The returned struct must not be modified.
  1108  func ptrto(t *Type) *Type {
  1109  	if Tptr == 0 {
  1110  		Fatalf("ptrto: no tptr")
  1111  	}
  1112  	if t == nil {
  1113  		Fatalf("ptrto: nil ptr")
  1114  	}
  1115  	return typPtr(t)
  1116  }
  1117  
  1118  func frame(context int) {
  1119  	if context != 0 {
  1120  		fmt.Printf("--- external frame ---\n")
  1121  		for _, n := range externdcl {
  1122  			printframenode(n)
  1123  		}
  1124  		return
  1125  	}
  1126  
  1127  	if Curfn != nil {
  1128  		fmt.Printf("--- %v frame ---\n", Curfn.Func.Nname.Sym)
  1129  		for _, ln := range Curfn.Func.Dcl {
  1130  			printframenode(ln)
  1131  		}
  1132  	}
  1133  }
  1134  
  1135  func printframenode(n *Node) {
  1136  	w := int64(-1)
  1137  	if n.Type != nil {
  1138  		w = n.Type.Width
  1139  	}
  1140  	switch n.Op {
  1141  	case ONAME:
  1142  		fmt.Printf("%v %v G%d %v width=%d\n", n.Op, n.Sym, n.Name.Vargen, n.Type, w)
  1143  	case OTYPE:
  1144  		fmt.Printf("%v %v width=%d\n", n.Op, n.Type, w)
  1145  	}
  1146  }
  1147  
  1148  // calculate sethi/ullman number
  1149  // roughly how many registers needed to
  1150  // compile a node. used to compile the
  1151  // hardest side first to minimize registers.
  1152  func ullmancalc(n *Node) {
  1153  	if n == nil {
  1154  		return
  1155  	}
  1156  
  1157  	var ul int
  1158  	var ur int
  1159  	if n.Ninit.Len() != 0 {
  1160  		ul = UINF
  1161  		goto out
  1162  	}
  1163  
  1164  	switch n.Op {
  1165  	case OLITERAL, ONAME:
  1166  		ul = 1
  1167  		if n.Class == PAUTOHEAP {
  1168  			ul++
  1169  		}
  1170  		goto out
  1171  
  1172  	case OCALL, OCALLFUNC, OCALLMETH, OCALLINTER, OASWB:
  1173  		ul = UINF
  1174  		goto out
  1175  
  1176  		// hard with instrumented code
  1177  	case OANDAND, OOROR:
  1178  		if instrumenting {
  1179  			ul = UINF
  1180  			goto out
  1181  		}
  1182  	case OINDEX, OSLICE, OSLICEARR, OSLICE3, OSLICE3ARR, OSLICESTR,
  1183  		OIND, ODOTPTR, ODOTTYPE, ODIV, OMOD:
  1184  		// These ops might panic, make sure they are done
  1185  		// before we start marshaling args for a call. See issue 16760.
  1186  		ul = UINF
  1187  		goto out
  1188  	}
  1189  
  1190  	ul = 1
  1191  	if n.Left != nil {
  1192  		ul = int(n.Left.Ullman)
  1193  	}
  1194  	ur = 1
  1195  	if n.Right != nil {
  1196  		ur = int(n.Right.Ullman)
  1197  	}
  1198  	if ul == ur {
  1199  		ul += 1
  1200  	}
  1201  	if ur > ul {
  1202  		ul = ur
  1203  	}
  1204  
  1205  out:
  1206  	if ul > 200 {
  1207  		ul = 200 // clamp to uchar with room to grow
  1208  	}
  1209  	n.Ullman = uint8(ul)
  1210  }
  1211  
  1212  func badtype(op Op, tl *Type, tr *Type) {
  1213  	fmt_ := ""
  1214  	if tl != nil {
  1215  		fmt_ += fmt.Sprintf("\n\t%v", tl)
  1216  	}
  1217  	if tr != nil {
  1218  		fmt_ += fmt.Sprintf("\n\t%v", tr)
  1219  	}
  1220  
  1221  	// common mistake: *struct and *interface.
  1222  	if tl != nil && tr != nil && tl.IsPtr() && tr.IsPtr() {
  1223  		if tl.Elem().IsStruct() && tr.Elem().IsInterface() {
  1224  			fmt_ += "\n\t(*struct vs *interface)"
  1225  		} else if tl.Elem().IsInterface() && tr.Elem().IsStruct() {
  1226  			fmt_ += "\n\t(*interface vs *struct)"
  1227  		}
  1228  	}
  1229  
  1230  	s := fmt_
  1231  	yyerror("illegal types for operand: %v%s", op, s)
  1232  }
  1233  
  1234  // brcom returns !(op).
  1235  // For example, brcom(==) is !=.
  1236  func brcom(op Op) Op {
  1237  	switch op {
  1238  	case OEQ:
  1239  		return ONE
  1240  	case ONE:
  1241  		return OEQ
  1242  	case OLT:
  1243  		return OGE
  1244  	case OGT:
  1245  		return OLE
  1246  	case OLE:
  1247  		return OGT
  1248  	case OGE:
  1249  		return OLT
  1250  	}
  1251  	Fatalf("brcom: no com for %v\n", op)
  1252  	return op
  1253  }
  1254  
  1255  // brrev returns reverse(op).
  1256  // For example, Brrev(<) is >.
  1257  func brrev(op Op) Op {
  1258  	switch op {
  1259  	case OEQ:
  1260  		return OEQ
  1261  	case ONE:
  1262  		return ONE
  1263  	case OLT:
  1264  		return OGT
  1265  	case OGT:
  1266  		return OLT
  1267  	case OLE:
  1268  		return OGE
  1269  	case OGE:
  1270  		return OLE
  1271  	}
  1272  	Fatalf("brrev: no rev for %v\n", op)
  1273  	return op
  1274  }
  1275  
  1276  // return side effect-free n, appending side effects to init.
  1277  // result is assignable if n is.
  1278  func safeexpr(n *Node, init *Nodes) *Node {
  1279  	if n == nil {
  1280  		return nil
  1281  	}
  1282  
  1283  	if n.Ninit.Len() != 0 {
  1284  		walkstmtlist(n.Ninit.Slice())
  1285  		init.AppendNodes(&n.Ninit)
  1286  	}
  1287  
  1288  	switch n.Op {
  1289  	case ONAME, OLITERAL:
  1290  		return n
  1291  
  1292  	case ODOT, OLEN, OCAP:
  1293  		l := safeexpr(n.Left, init)
  1294  		if l == n.Left {
  1295  			return n
  1296  		}
  1297  		r := nod(OXXX, nil, nil)
  1298  		*r = *n
  1299  		r.Left = l
  1300  		r = typecheck(r, Erv)
  1301  		r = walkexpr(r, init)
  1302  		return r
  1303  
  1304  	case ODOTPTR, OIND:
  1305  		l := safeexpr(n.Left, init)
  1306  		if l == n.Left {
  1307  			return n
  1308  		}
  1309  		a := nod(OXXX, nil, nil)
  1310  		*a = *n
  1311  		a.Left = l
  1312  		a = walkexpr(a, init)
  1313  		return a
  1314  
  1315  	case OINDEX, OINDEXMAP:
  1316  		l := safeexpr(n.Left, init)
  1317  		r := safeexpr(n.Right, init)
  1318  		if l == n.Left && r == n.Right {
  1319  			return n
  1320  		}
  1321  		a := nod(OXXX, nil, nil)
  1322  		*a = *n
  1323  		a.Left = l
  1324  		a.Right = r
  1325  		a = walkexpr(a, init)
  1326  		return a
  1327  
  1328  	case OSTRUCTLIT, OARRAYLIT, OSLICELIT:
  1329  		if isStaticCompositeLiteral(n) {
  1330  			return n
  1331  		}
  1332  	}
  1333  
  1334  	// make a copy; must not be used as an lvalue
  1335  	if islvalue(n) {
  1336  		Fatalf("missing lvalue case in safeexpr: %v", n)
  1337  	}
  1338  	return cheapexpr(n, init)
  1339  }
  1340  
  1341  func copyexpr(n *Node, t *Type, init *Nodes) *Node {
  1342  	l := temp(t)
  1343  	a := nod(OAS, l, n)
  1344  	a = typecheck(a, Etop)
  1345  	a = walkexpr(a, init)
  1346  	init.Append(a)
  1347  	return l
  1348  }
  1349  
  1350  // return side-effect free and cheap n, appending side effects to init.
  1351  // result may not be assignable.
  1352  func cheapexpr(n *Node, init *Nodes) *Node {
  1353  	switch n.Op {
  1354  	case ONAME, OLITERAL:
  1355  		return n
  1356  	}
  1357  
  1358  	return copyexpr(n, n.Type, init)
  1359  }
  1360  
  1361  // Code to resolve elided DOTs in embedded types.
  1362  
  1363  // A Dlist stores a pointer to a TFIELD Type embedded within
  1364  // a TSTRUCT or TINTER Type.
  1365  type Dlist struct {
  1366  	field *Field
  1367  }
  1368  
  1369  // dotlist is used by adddot1 to record the path of embedded fields
  1370  // used to access a target field or method.
  1371  // Must be non-nil so that dotpath returns a non-nil slice even if d is zero.
  1372  var dotlist = make([]Dlist, 10)
  1373  
  1374  // lookdot0 returns the number of fields or methods named s associated
  1375  // with Type t. If exactly one exists, it will be returned in *save
  1376  // (if save is not nil).
  1377  func lookdot0(s *Sym, t *Type, save **Field, ignorecase bool) int {
  1378  	u := t
  1379  	if u.IsPtr() {
  1380  		u = u.Elem()
  1381  	}
  1382  
  1383  	c := 0
  1384  	if u.IsStruct() || u.IsInterface() {
  1385  		for _, f := range u.Fields().Slice() {
  1386  			if f.Sym == s || (ignorecase && f.Type.Etype == TFUNC && f.Type.Recv() != nil && strings.EqualFold(f.Sym.Name, s.Name)) {
  1387  				if save != nil {
  1388  					*save = f
  1389  				}
  1390  				c++
  1391  			}
  1392  		}
  1393  	}
  1394  
  1395  	u = methtype(t)
  1396  	if u != nil {
  1397  		for _, f := range u.Methods().Slice() {
  1398  			if f.Embedded == 0 && (f.Sym == s || (ignorecase && strings.EqualFold(f.Sym.Name, s.Name))) {
  1399  				if save != nil {
  1400  					*save = f
  1401  				}
  1402  				c++
  1403  			}
  1404  		}
  1405  	}
  1406  
  1407  	return c
  1408  }
  1409  
  1410  // adddot1 returns the number of fields or methods named s at depth d in Type t.
  1411  // If exactly one exists, it will be returned in *save (if save is not nil),
  1412  // and dotlist will contain the path of embedded fields traversed to find it,
  1413  // in reverse order. If none exist, more will indicate whether t contains any
  1414  // embedded fields at depth d, so callers can decide whether to retry at
  1415  // a greater depth.
  1416  func adddot1(s *Sym, t *Type, d int, save **Field, ignorecase bool) (c int, more bool) {
  1417  	if t.Trecur != 0 {
  1418  		return
  1419  	}
  1420  	t.Trecur = 1
  1421  
  1422  	var u *Type
  1423  	d--
  1424  	if d < 0 {
  1425  		// We've reached our target depth. If t has any fields/methods
  1426  		// named s, then we're done. Otherwise, we still need to check
  1427  		// below for embedded fields.
  1428  		c = lookdot0(s, t, save, ignorecase)
  1429  		if c != 0 {
  1430  			goto out
  1431  		}
  1432  	}
  1433  
  1434  	u = t
  1435  	if u.IsPtr() {
  1436  		u = u.Elem()
  1437  	}
  1438  	if !u.IsStruct() && !u.IsInterface() {
  1439  		goto out
  1440  	}
  1441  
  1442  	for _, f := range u.Fields().Slice() {
  1443  		if f.Embedded == 0 || f.Sym == nil {
  1444  			continue
  1445  		}
  1446  		if d < 0 {
  1447  			// Found an embedded field at target depth.
  1448  			more = true
  1449  			goto out
  1450  		}
  1451  		a, more1 := adddot1(s, f.Type, d, save, ignorecase)
  1452  		if a != 0 && c == 0 {
  1453  			dotlist[d].field = f
  1454  		}
  1455  		c += a
  1456  		if more1 {
  1457  			more = true
  1458  		}
  1459  	}
  1460  
  1461  out:
  1462  	t.Trecur = 0
  1463  	return c, more
  1464  }
  1465  
  1466  // dotpath computes the unique shortest explicit selector path to fully qualify
  1467  // a selection expression x.f, where x is of type t and f is the symbol s.
  1468  // If no such path exists, dotpath returns nil.
  1469  // If there are multiple shortest paths to the same depth, ambig is true.
  1470  func dotpath(s *Sym, t *Type, save **Field, ignorecase bool) (path []Dlist, ambig bool) {
  1471  	// The embedding of types within structs imposes a tree structure onto
  1472  	// types: structs parent the types they embed, and types parent their
  1473  	// fields or methods. Our goal here is to find the shortest path to
  1474  	// a field or method named s in the subtree rooted at t. To accomplish
  1475  	// that, we iteratively perform depth-first searches of increasing depth
  1476  	// until we either find the named field/method or exhaust the tree.
  1477  	for d := 0; ; d++ {
  1478  		if d > len(dotlist) {
  1479  			dotlist = append(dotlist, Dlist{})
  1480  		}
  1481  		if c, more := adddot1(s, t, d, save, ignorecase); c == 1 {
  1482  			return dotlist[:d], false
  1483  		} else if c > 1 {
  1484  			return nil, true
  1485  		} else if !more {
  1486  			return nil, false
  1487  		}
  1488  	}
  1489  }
  1490  
  1491  // in T.field
  1492  // find missing fields that
  1493  // will give shortest unique addressing.
  1494  // modify the tree with missing type names.
  1495  func adddot(n *Node) *Node {
  1496  	n.Left = typecheck(n.Left, Etype|Erv)
  1497  	if n.Left.Diag {
  1498  		n.Diag = true
  1499  	}
  1500  	t := n.Left.Type
  1501  	if t == nil {
  1502  		return n
  1503  	}
  1504  
  1505  	if n.Left.Op == OTYPE {
  1506  		return n
  1507  	}
  1508  
  1509  	s := n.Sym
  1510  	if s == nil {
  1511  		return n
  1512  	}
  1513  
  1514  	switch path, ambig := dotpath(s, t, nil, false); {
  1515  	case path != nil:
  1516  		// rebuild elided dots
  1517  		for c := len(path) - 1; c >= 0; c-- {
  1518  			n.Left = nodSym(ODOT, n.Left, path[c].field.Sym)
  1519  			n.Left.Implicit = true
  1520  		}
  1521  	case ambig:
  1522  		yyerror("ambiguous selector %v", n)
  1523  		n.Left = nil
  1524  	}
  1525  
  1526  	return n
  1527  }
  1528  
  1529  // code to help generate trampoline
  1530  // functions for methods on embedded
  1531  // subtypes.
  1532  // these are approx the same as
  1533  // the corresponding adddot routines
  1534  // except that they expect to be called
  1535  // with unique tasks and they return
  1536  // the actual methods.
  1537  type Symlink struct {
  1538  	field     *Field
  1539  	followptr bool
  1540  }
  1541  
  1542  var slist []Symlink
  1543  
  1544  func expand0(t *Type, followptr bool) {
  1545  	u := t
  1546  	if u.IsPtr() {
  1547  		followptr = true
  1548  		u = u.Elem()
  1549  	}
  1550  
  1551  	if u.IsInterface() {
  1552  		for _, f := range u.Fields().Slice() {
  1553  			if f.Sym.Flags&SymUniq != 0 {
  1554  				continue
  1555  			}
  1556  			f.Sym.Flags |= SymUniq
  1557  			slist = append(slist, Symlink{field: f, followptr: followptr})
  1558  		}
  1559  
  1560  		return
  1561  	}
  1562  
  1563  	u = methtype(t)
  1564  	if u != nil {
  1565  		for _, f := range u.Methods().Slice() {
  1566  			if f.Sym.Flags&SymUniq != 0 {
  1567  				continue
  1568  			}
  1569  			f.Sym.Flags |= SymUniq
  1570  			slist = append(slist, Symlink{field: f, followptr: followptr})
  1571  		}
  1572  	}
  1573  }
  1574  
  1575  func expand1(t *Type, top, followptr bool) {
  1576  	if t.Trecur != 0 {
  1577  		return
  1578  	}
  1579  	t.Trecur = 1
  1580  
  1581  	if !top {
  1582  		expand0(t, followptr)
  1583  	}
  1584  
  1585  	u := t
  1586  	if u.IsPtr() {
  1587  		followptr = true
  1588  		u = u.Elem()
  1589  	}
  1590  
  1591  	if !u.IsStruct() && !u.IsInterface() {
  1592  		goto out
  1593  	}
  1594  
  1595  	for _, f := range u.Fields().Slice() {
  1596  		if f.Embedded == 0 {
  1597  			continue
  1598  		}
  1599  		if f.Sym == nil {
  1600  			continue
  1601  		}
  1602  		expand1(f.Type, false, followptr)
  1603  	}
  1604  
  1605  out:
  1606  	t.Trecur = 0
  1607  }
  1608  
  1609  func expandmeth(t *Type) {
  1610  	if t == nil || t.AllMethods().Len() != 0 {
  1611  		return
  1612  	}
  1613  
  1614  	// mark top-level method symbols
  1615  	// so that expand1 doesn't consider them.
  1616  	for _, f := range t.Methods().Slice() {
  1617  		f.Sym.Flags |= SymUniq
  1618  	}
  1619  
  1620  	// generate all reachable methods
  1621  	slist = slist[:0]
  1622  	expand1(t, true, false)
  1623  
  1624  	// check each method to be uniquely reachable
  1625  	var ms []*Field
  1626  	for i, sl := range slist {
  1627  		slist[i].field = nil
  1628  		sl.field.Sym.Flags &^= SymUniq
  1629  
  1630  		var f *Field
  1631  		if path, _ := dotpath(sl.field.Sym, t, &f, false); path == nil {
  1632  			continue
  1633  		}
  1634  
  1635  		// dotpath may have dug out arbitrary fields, we only want methods.
  1636  		if f.Type.Etype != TFUNC || f.Type.Recv() == nil {
  1637  			continue
  1638  		}
  1639  
  1640  		// add it to the base type method list
  1641  		f = f.Copy()
  1642  		f.Embedded = 1 // needs a trampoline
  1643  		if sl.followptr {
  1644  			f.Embedded = 2
  1645  		}
  1646  		ms = append(ms, f)
  1647  	}
  1648  
  1649  	for _, f := range t.Methods().Slice() {
  1650  		f.Sym.Flags &^= SymUniq
  1651  	}
  1652  
  1653  	ms = append(ms, t.Methods().Slice()...)
  1654  	t.AllMethods().Set(ms)
  1655  }
  1656  
  1657  // Given funarg struct list, return list of ODCLFIELD Node fn args.
  1658  func structargs(tl *Type, mustname bool) []*Node {
  1659  	var args []*Node
  1660  	gen := 0
  1661  	for _, t := range tl.Fields().Slice() {
  1662  		var n *Node
  1663  		if mustname && (t.Sym == nil || t.Sym.Name == "_") {
  1664  			// invent a name so that we can refer to it in the trampoline
  1665  			buf := fmt.Sprintf(".anon%d", gen)
  1666  			gen++
  1667  			n = newname(lookup(buf))
  1668  		} else if t.Sym != nil {
  1669  			n = newname(t.Sym)
  1670  		}
  1671  		a := nod(ODCLFIELD, n, typenod(t.Type))
  1672  		a.Isddd = t.Isddd
  1673  		if n != nil {
  1674  			n.Isddd = t.Isddd
  1675  		}
  1676  		args = append(args, a)
  1677  	}
  1678  
  1679  	return args
  1680  }
  1681  
  1682  // Generate a wrapper function to convert from
  1683  // a receiver of type T to a receiver of type U.
  1684  // That is,
  1685  //
  1686  //	func (t T) M() {
  1687  //		...
  1688  //	}
  1689  //
  1690  // already exists; this function generates
  1691  //
  1692  //	func (u U) M() {
  1693  //		u.M()
  1694  //	}
  1695  //
  1696  // where the types T and U are such that u.M() is valid
  1697  // and calls the T.M method.
  1698  // The resulting function is for use in method tables.
  1699  //
  1700  //	rcvr - U
  1701  //	method - M func (t T)(), a TFIELD type struct
  1702  //	newnam - the eventual mangled name of this function
  1703  
  1704  func genwrapper(rcvr *Type, method *Field, newnam *Sym, iface int) {
  1705  	if false && Debug['r'] != 0 {
  1706  		fmt.Printf("genwrapper rcvrtype=%v method=%v newnam=%v\n", rcvr, method, newnam)
  1707  	}
  1708  
  1709  	lineno = MakePos(src.NewFileBase("<autogenerated>", "<autogenerated>"), 1, 0)
  1710  
  1711  	dclcontext = PEXTERN
  1712  	markdcl()
  1713  
  1714  	this := nod(ODCLFIELD, newname(lookup(".this")), typenod(rcvr))
  1715  	this.Left.Name.Param.Ntype = this.Right
  1716  	in := structargs(method.Type.Params(), true)
  1717  	out := structargs(method.Type.Results(), false)
  1718  
  1719  	t := nod(OTFUNC, nil, nil)
  1720  	l := []*Node{this}
  1721  	if iface != 0 && rcvr.Width < Types[Tptr].Width {
  1722  		// Building method for interface table and receiver
  1723  		// is smaller than the single pointer-sized word
  1724  		// that the interface call will pass in.
  1725  		// Add a dummy padding argument after the
  1726  		// receiver to make up the difference.
  1727  		tpad := typArray(Types[TUINT8], Types[Tptr].Width-rcvr.Width)
  1728  		pad := nod(ODCLFIELD, newname(lookup(".pad")), typenod(tpad))
  1729  		l = append(l, pad)
  1730  	}
  1731  
  1732  	t.List.Set(append(l, in...))
  1733  	t.Rlist.Set(out)
  1734  
  1735  	fn := nod(ODCLFUNC, nil, nil)
  1736  	fn.Func.Nname = newname(newnam)
  1737  	fn.Func.Nname.Name.Defn = fn
  1738  	fn.Func.Nname.Name.Param.Ntype = t
  1739  	declare(fn.Func.Nname, PFUNC)
  1740  	funchdr(fn)
  1741  
  1742  	// arg list
  1743  	var args []*Node
  1744  
  1745  	isddd := false
  1746  	for _, n := range in {
  1747  		args = append(args, n.Left)
  1748  		isddd = n.Left.Isddd
  1749  	}
  1750  
  1751  	methodrcvr := method.Type.Recv().Type
  1752  
  1753  	// generate nil pointer check for better error
  1754  	if rcvr.IsPtr() && rcvr.Elem() == methodrcvr {
  1755  		// generating wrapper from *T to T.
  1756  		n := nod(OIF, nil, nil)
  1757  
  1758  		n.Left = nod(OEQ, this.Left, nodnil())
  1759  
  1760  		// these strings are already in the reflect tables,
  1761  		// so no space cost to use them here.
  1762  		var l []*Node
  1763  
  1764  		var v Val
  1765  		v.U = rcvr.Elem().Sym.Pkg.Name // package name
  1766  		l = append(l, nodlit(v))
  1767  		v.U = rcvr.Elem().Sym.Name // type name
  1768  		l = append(l, nodlit(v))
  1769  		v.U = method.Sym.Name
  1770  		l = append(l, nodlit(v)) // method name
  1771  		call := nod(OCALL, syslook("panicwrap"), nil)
  1772  		call.List.Set(l)
  1773  		n.Nbody.Set1(call)
  1774  		fn.Nbody.Append(n)
  1775  	}
  1776  
  1777  	dot := adddot(nodSym(OXDOT, this.Left, method.Sym))
  1778  
  1779  	// generate call
  1780  	// It's not possible to use a tail call when dynamic linking on ppc64le. The
  1781  	// bad scenario is when a local call is made to the wrapper: the wrapper will
  1782  	// call the implementation, which might be in a different module and so set
  1783  	// the TOC to the appropriate value for that module. But if it returns
  1784  	// directly to the wrapper's caller, nothing will reset it to the correct
  1785  	// value for that function.
  1786  	if !instrumenting && rcvr.IsPtr() && methodrcvr.IsPtr() && method.Embedded != 0 && !isifacemethod(method.Type) && !(Thearch.LinkArch.Name == "ppc64le" && Ctxt.Flag_dynlink) {
  1787  		// generate tail call: adjust pointer receiver and jump to embedded method.
  1788  		dot = dot.Left // skip final .M
  1789  		// TODO(mdempsky): Remove dependency on dotlist.
  1790  		if !dotlist[0].field.Type.IsPtr() {
  1791  			dot = nod(OADDR, dot, nil)
  1792  		}
  1793  		as := nod(OAS, this.Left, nod(OCONVNOP, dot, nil))
  1794  		as.Right.Type = rcvr
  1795  		fn.Nbody.Append(as)
  1796  		n := nod(ORETJMP, nil, nil)
  1797  		n.Left = newname(methodsym(method.Sym, methodrcvr, 0))
  1798  		fn.Nbody.Append(n)
  1799  		// When tail-calling, we can't use a frame pointer.
  1800  		fn.Func.NoFramePointer = true
  1801  	} else {
  1802  		fn.Func.Wrapper = true // ignore frame for panic+recover matching
  1803  		call := nod(OCALL, dot, nil)
  1804  		call.List.Set(args)
  1805  		call.Isddd = isddd
  1806  		if method.Type.Results().NumFields() > 0 {
  1807  			n := nod(ORETURN, nil, nil)
  1808  			n.List.Set1(call)
  1809  			call = n
  1810  		}
  1811  
  1812  		fn.Nbody.Append(call)
  1813  	}
  1814  
  1815  	if false && Debug['r'] != 0 {
  1816  		dumplist("genwrapper body", fn.Nbody)
  1817  	}
  1818  
  1819  	funcbody(fn)
  1820  	Curfn = fn
  1821  	popdcl()
  1822  	if debug_dclstack != 0 {
  1823  		testdclstack()
  1824  	}
  1825  
  1826  	// wrappers where T is anonymous (struct or interface) can be duplicated.
  1827  	if rcvr.IsStruct() || rcvr.IsInterface() || rcvr.IsPtr() && rcvr.Elem().IsStruct() {
  1828  		fn.Func.Dupok = true
  1829  	}
  1830  	fn = typecheck(fn, Etop)
  1831  	typecheckslice(fn.Nbody.Slice(), Etop)
  1832  
  1833  	inlcalls(fn)
  1834  	escAnalyze([]*Node{fn}, false)
  1835  
  1836  	Curfn = nil
  1837  	funccompile(fn)
  1838  }
  1839  
  1840  func hashmem(t *Type) *Node {
  1841  	sym := Pkglookup("memhash", Runtimepkg)
  1842  
  1843  	n := newname(sym)
  1844  	n.Class = PFUNC
  1845  	tfn := nod(OTFUNC, nil, nil)
  1846  	tfn.List.Append(nod(ODCLFIELD, nil, typenod(ptrto(t))))
  1847  	tfn.List.Append(nod(ODCLFIELD, nil, typenod(Types[TUINTPTR])))
  1848  	tfn.List.Append(nod(ODCLFIELD, nil, typenod(Types[TUINTPTR])))
  1849  	tfn.Rlist.Append(nod(ODCLFIELD, nil, typenod(Types[TUINTPTR])))
  1850  	tfn = typecheck(tfn, Etype)
  1851  	n.Type = tfn.Type
  1852  	return n
  1853  }
  1854  
  1855  func ifacelookdot(s *Sym, t *Type, followptr *bool, ignorecase bool) *Field {
  1856  	*followptr = false
  1857  
  1858  	if t == nil {
  1859  		return nil
  1860  	}
  1861  
  1862  	var m *Field
  1863  	path, ambig := dotpath(s, t, &m, ignorecase)
  1864  	if path == nil {
  1865  		if ambig {
  1866  			yyerror("%v.%v is ambiguous", t, s)
  1867  		}
  1868  		return nil
  1869  	}
  1870  
  1871  	for _, d := range path {
  1872  		if d.field.Type.IsPtr() {
  1873  			*followptr = true
  1874  			break
  1875  		}
  1876  	}
  1877  
  1878  	if m.Type.Etype != TFUNC || m.Type.Recv() == nil {
  1879  		yyerror("%v.%v is a field, not a method", t, s)
  1880  		return nil
  1881  	}
  1882  
  1883  	return m
  1884  }
  1885  
  1886  func implements(t, iface *Type, m, samename **Field, ptr *int) bool {
  1887  	t0 := t
  1888  	if t == nil {
  1889  		return false
  1890  	}
  1891  
  1892  	// if this is too slow,
  1893  	// could sort these first
  1894  	// and then do one loop.
  1895  
  1896  	if t.IsInterface() {
  1897  		for _, im := range iface.Fields().Slice() {
  1898  			for _, tm := range t.Fields().Slice() {
  1899  				if tm.Sym == im.Sym {
  1900  					if eqtype(tm.Type, im.Type) {
  1901  						goto found
  1902  					}
  1903  					*m = im
  1904  					*samename = tm
  1905  					*ptr = 0
  1906  					return false
  1907  				}
  1908  			}
  1909  
  1910  			*m = im
  1911  			*samename = nil
  1912  			*ptr = 0
  1913  			return false
  1914  		found:
  1915  		}
  1916  
  1917  		return true
  1918  	}
  1919  
  1920  	t = methtype(t)
  1921  	if t != nil {
  1922  		expandmeth(t)
  1923  	}
  1924  	for _, im := range iface.Fields().Slice() {
  1925  		if im.Broke {
  1926  			continue
  1927  		}
  1928  		var followptr bool
  1929  		tm := ifacelookdot(im.Sym, t, &followptr, false)
  1930  		if tm == nil || tm.Nointerface || !eqtype(tm.Type, im.Type) {
  1931  			if tm == nil {
  1932  				tm = ifacelookdot(im.Sym, t, &followptr, true)
  1933  			}
  1934  			*m = im
  1935  			*samename = tm
  1936  			*ptr = 0
  1937  			return false
  1938  		}
  1939  
  1940  		// if pointer receiver in method,
  1941  		// the method does not exist for value types.
  1942  		rcvr := tm.Type.Recv().Type
  1943  
  1944  		if rcvr.IsPtr() && !t0.IsPtr() && !followptr && !isifacemethod(tm.Type) {
  1945  			if false && Debug['r'] != 0 {
  1946  				yyerror("interface pointer mismatch")
  1947  			}
  1948  
  1949  			*m = im
  1950  			*samename = nil
  1951  			*ptr = 1
  1952  			return false
  1953  		}
  1954  	}
  1955  
  1956  	return true
  1957  }
  1958  
  1959  // even simpler simtype; get rid of ptr, bool.
  1960  // assuming that the front end has rejected
  1961  // all the invalid conversions (like ptr -> bool)
  1962  func Simsimtype(t *Type) EType {
  1963  	if t == nil {
  1964  		return 0
  1965  	}
  1966  
  1967  	et := simtype[t.Etype]
  1968  	switch et {
  1969  	case TPTR32:
  1970  		et = TUINT32
  1971  
  1972  	case TPTR64:
  1973  		et = TUINT64
  1974  
  1975  	case TBOOL:
  1976  		et = TUINT8
  1977  	}
  1978  
  1979  	return et
  1980  }
  1981  
  1982  func listtreecopy(l []*Node, pos src.XPos) []*Node {
  1983  	var out []*Node
  1984  	for _, n := range l {
  1985  		out = append(out, treecopy(n, pos))
  1986  	}
  1987  	return out
  1988  }
  1989  
  1990  func liststmt(l []*Node) *Node {
  1991  	n := nod(OBLOCK, nil, nil)
  1992  	n.List.Set(l)
  1993  	if len(l) != 0 {
  1994  		n.Pos = l[0].Pos
  1995  	}
  1996  	return n
  1997  }
  1998  
  1999  // return power of 2 of the constant
  2000  // operand. -1 if it is not a power of 2.
  2001  // 1000+ if it is a -(power of 2)
  2002  func powtwo(n *Node) int {
  2003  	if n == nil || n.Op != OLITERAL || n.Type == nil {
  2004  		return -1
  2005  	}
  2006  	if !n.Type.IsInteger() {
  2007  		return -1
  2008  	}
  2009  
  2010  	v := uint64(n.Int64())
  2011  	b := uint64(1)
  2012  	for i := 0; i < 64; i++ {
  2013  		if b == v {
  2014  			return i
  2015  		}
  2016  		b = b << 1
  2017  	}
  2018  
  2019  	if !n.Type.IsSigned() {
  2020  		return -1
  2021  	}
  2022  
  2023  	v = -v
  2024  	b = 1
  2025  	for i := 0; i < 64; i++ {
  2026  		if b == v {
  2027  			return i + 1000
  2028  		}
  2029  		b = b << 1
  2030  	}
  2031  
  2032  	return -1
  2033  }
  2034  
  2035  func ngotype(n *Node) *Sym {
  2036  	if n.Type != nil {
  2037  		return typenamesym(n.Type)
  2038  	}
  2039  	return nil
  2040  }
  2041  
  2042  // Convert raw string to the prefix that will be used in the symbol
  2043  // table. All control characters, space, '%' and '"', as well as
  2044  // non-7-bit clean bytes turn into %xx. The period needs escaping
  2045  // only in the last segment of the path, and it makes for happier
  2046  // users if we escape that as little as possible.
  2047  //
  2048  // If you edit this, edit ../../debug/goobj/read.go:/importPathToPrefix too.
  2049  func pathtoprefix(s string) string {
  2050  	slash := strings.LastIndex(s, "/")
  2051  	for i := 0; i < len(s); i++ {
  2052  		c := s[i]
  2053  		if c <= ' ' || i >= slash && c == '.' || c == '%' || c == '"' || c >= 0x7F {
  2054  			var buf bytes.Buffer
  2055  			for i := 0; i < len(s); i++ {
  2056  				c := s[i]
  2057  				if c <= ' ' || i >= slash && c == '.' || c == '%' || c == '"' || c >= 0x7F {
  2058  					fmt.Fprintf(&buf, "%%%02x", c)
  2059  					continue
  2060  				}
  2061  				buf.WriteByte(c)
  2062  			}
  2063  			return buf.String()
  2064  		}
  2065  	}
  2066  	return s
  2067  }
  2068  
  2069  var pkgMap = make(map[string]*Pkg)
  2070  var pkgs []*Pkg
  2071  
  2072  func mkpkg(path string) *Pkg {
  2073  	if p := pkgMap[path]; p != nil {
  2074  		return p
  2075  	}
  2076  
  2077  	p := new(Pkg)
  2078  	p.Path = path
  2079  	p.Prefix = pathtoprefix(path)
  2080  	p.Syms = make(map[string]*Sym)
  2081  	pkgMap[path] = p
  2082  	pkgs = append(pkgs, p)
  2083  	return p
  2084  }
  2085  
  2086  // The result of addinit MUST be assigned back to n, e.g.
  2087  // 	n.Left = addinit(n.Left, init)
  2088  func addinit(n *Node, init []*Node) *Node {
  2089  	if len(init) == 0 {
  2090  		return n
  2091  	}
  2092  
  2093  	switch n.Op {
  2094  	// There may be multiple refs to this node;
  2095  	// introduce OCONVNOP to hold init list.
  2096  	case ONAME, OLITERAL:
  2097  		n = nod(OCONVNOP, n, nil)
  2098  		n.Type = n.Left.Type
  2099  		n.Typecheck = 1
  2100  	}
  2101  
  2102  	n.Ninit.Prepend(init...)
  2103  	n.Ullman = UINF
  2104  	return n
  2105  }
  2106  
  2107  var reservedimports = []string{
  2108  	"go",
  2109  	"type",
  2110  }
  2111  
  2112  func isbadimport(path string) bool {
  2113  	if strings.Contains(path, "\x00") {
  2114  		yyerror("import path contains NUL")
  2115  		return true
  2116  	}
  2117  
  2118  	for _, ri := range reservedimports {
  2119  		if path == ri {
  2120  			yyerror("import path %q is reserved and cannot be used", path)
  2121  			return true
  2122  		}
  2123  	}
  2124  
  2125  	for _, r := range path {
  2126  		if r == utf8.RuneError {
  2127  			yyerror("import path contains invalid UTF-8 sequence: %q", path)
  2128  			return true
  2129  		}
  2130  
  2131  		if r < 0x20 || r == 0x7f {
  2132  			yyerror("import path contains control character: %q", path)
  2133  			return true
  2134  		}
  2135  
  2136  		if r == '\\' {
  2137  			yyerror("import path contains backslash; use slash: %q", path)
  2138  			return true
  2139  		}
  2140  
  2141  		if unicode.IsSpace(r) {
  2142  			yyerror("import path contains space character: %q", path)
  2143  			return true
  2144  		}
  2145  
  2146  		if strings.ContainsRune("!\"#$%&'()*,:;<=>?[]^`{|}", r) {
  2147  			yyerror("import path contains invalid character '%c': %q", r, path)
  2148  			return true
  2149  		}
  2150  	}
  2151  
  2152  	return false
  2153  }
  2154  
  2155  func checknil(x *Node, init *Nodes) {
  2156  	x = walkexpr(x, nil) // caller has not done this yet
  2157  	if x.Type.IsInterface() {
  2158  		x = nod(OITAB, x, nil)
  2159  		x = typecheck(x, Erv)
  2160  	}
  2161  
  2162  	n := nod(OCHECKNIL, x, nil)
  2163  	n.Typecheck = 1
  2164  	init.Append(n)
  2165  }
  2166  
  2167  // Can this type be stored directly in an interface word?
  2168  // Yes, if the representation is a single pointer.
  2169  func isdirectiface(t *Type) bool {
  2170  	switch t.Etype {
  2171  	case TPTR32,
  2172  		TPTR64,
  2173  		TCHAN,
  2174  		TMAP,
  2175  		TFUNC,
  2176  		TUNSAFEPTR:
  2177  		return true
  2178  
  2179  	case TARRAY:
  2180  		// Array of 1 direct iface type can be direct.
  2181  		return t.NumElem() == 1 && isdirectiface(t.Elem())
  2182  
  2183  	case TSTRUCT:
  2184  		// Struct with 1 field of direct iface type can be direct.
  2185  		return t.NumFields() == 1 && isdirectiface(t.Field(0).Type)
  2186  	}
  2187  
  2188  	return false
  2189  }
  2190  
  2191  // itabType loads the _type field from a runtime.itab struct.
  2192  func itabType(itab *Node) *Node {
  2193  	typ := nodSym(ODOTPTR, itab, nil)
  2194  	typ.Type = ptrto(Types[TUINT8])
  2195  	typ.Typecheck = 1
  2196  	typ.Xoffset = int64(Widthptr) // offset of _type in runtime.itab
  2197  	typ.Bounded = true            // guaranteed not to fault
  2198  	return typ
  2199  }
  2200  
  2201  // ifaceData loads the data field from an interface.
  2202  // The concrete type must be known to have type t.
  2203  // It follows the pointer if !isdirectiface(t).
  2204  func ifaceData(n *Node, t *Type) *Node {
  2205  	ptr := nodSym(OIDATA, n, nil)
  2206  	if isdirectiface(t) {
  2207  		ptr.Type = t
  2208  		ptr.Typecheck = 1
  2209  		return ptr
  2210  	}
  2211  	ptr.Type = ptrto(t)
  2212  	ptr.Bounded = true
  2213  	ptr.Typecheck = 1
  2214  	ind := nod(OIND, ptr, nil)
  2215  	ind.Type = t
  2216  	ind.Typecheck = 1
  2217  	return ind
  2218  }
  2219  
  2220  // iet returns 'T' if t is a concrete type,
  2221  // 'I' if t is an interface type, and 'E' if t is an empty interface type.
  2222  // It is used to build calls to the conv* and assert* runtime routines.
  2223  func (t *Type) iet() byte {
  2224  	if t.IsEmptyInterface() {
  2225  		return 'E'
  2226  	}
  2227  	if t.IsInterface() {
  2228  		return 'I'
  2229  	}
  2230  	return 'T'
  2231  }