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