Source file src/go/types/typestring.go

     1  // Code generated by "go test -run=Generate -write=all"; DO NOT EDIT.
     2  // Source: ../../cmd/compile/internal/types2/typestring.go
     3  
     4  // Copyright 2013 The Go Authors. All rights reserved.
     5  // Use of this source code is governed by a BSD-style
     6  // license that can be found in the LICENSE file.
     7  
     8  // This file implements printing of types.
     9  
    10  package types
    11  
    12  import (
    13  	"bytes"
    14  	"fmt"
    15  	"sort"
    16  	"strconv"
    17  	"strings"
    18  	"unicode/utf8"
    19  )
    20  
    21  // A Qualifier controls how named package-level objects are printed in
    22  // calls to [TypeString], [ObjectString], and [SelectionString].
    23  //
    24  // These three formatting routines call the Qualifier for each
    25  // package-level object O, and if the Qualifier returns a non-empty
    26  // string p, the object is printed in the form p.O.
    27  // If it returns an empty string, only the object name O is printed.
    28  //
    29  // Using a nil Qualifier is equivalent to using (*[Package]).Path: the
    30  // object is qualified by the import path, e.g., "encoding/json.Marshal".
    31  type Qualifier func(*Package) string
    32  
    33  // RelativeTo returns a [Qualifier] that fully qualifies members of
    34  // all packages other than pkg.
    35  func RelativeTo(pkg *Package) Qualifier {
    36  	if pkg == nil {
    37  		return nil
    38  	}
    39  	return func(other *Package) string {
    40  		if pkg == other {
    41  			return "" // same package; unqualified
    42  		}
    43  		return other.Path()
    44  	}
    45  }
    46  
    47  // TypeString returns the string representation of typ.
    48  // The [Qualifier] controls the printing of
    49  // package-level objects, and may be nil.
    50  func TypeString(typ Type, qf Qualifier) string {
    51  	var buf bytes.Buffer
    52  	WriteType(&buf, typ, qf)
    53  	return buf.String()
    54  }
    55  
    56  // WriteType writes the string representation of typ to buf.
    57  // The [Qualifier] controls the printing of
    58  // package-level objects, and may be nil.
    59  func WriteType(buf *bytes.Buffer, typ Type, qf Qualifier) {
    60  	newTypeWriter(buf, qf).typ(typ)
    61  }
    62  
    63  // WriteSignature writes the representation of the signature sig to buf,
    64  // without a leading "func" keyword. The [Qualifier] controls the printing
    65  // of package-level objects, and may be nil.
    66  func WriteSignature(buf *bytes.Buffer, sig *Signature, qf Qualifier) {
    67  	newTypeWriter(buf, qf).signature(sig)
    68  }
    69  
    70  type typeWriter struct {
    71  	buf          *bytes.Buffer
    72  	seen         map[Type]bool
    73  	qf           Qualifier
    74  	ctxt         *Context       // if non-nil, we are type hashing
    75  	tparams      *TypeParamList // local type parameters
    76  	paramNames   bool           // if set, write function parameter names, otherwise, write types only
    77  	tpSubscripts bool           // if set, write type parameter indices as subscripts
    78  	pkgInfo      bool           // package-annotate first unexported-type field to avoid confusing type description
    79  }
    80  
    81  func newTypeWriter(buf *bytes.Buffer, qf Qualifier) *typeWriter {
    82  	return &typeWriter{buf, make(map[Type]bool), qf, nil, nil, true, false, false}
    83  }
    84  
    85  func newTypeHasher(buf *bytes.Buffer, ctxt *Context) *typeWriter {
    86  	assert(ctxt != nil)
    87  	return &typeWriter{buf, make(map[Type]bool), nil, ctxt, nil, false, false, false}
    88  }
    89  
    90  func (w *typeWriter) byte(b byte) {
    91  	if w.ctxt != nil {
    92  		if b == ' ' {
    93  			b = '#'
    94  		}
    95  		w.buf.WriteByte(b)
    96  		return
    97  	}
    98  	w.buf.WriteByte(b)
    99  	if b == ',' || b == ';' {
   100  		w.buf.WriteByte(' ')
   101  	}
   102  }
   103  
   104  func (w *typeWriter) string(s string) {
   105  	w.buf.WriteString(s)
   106  }
   107  
   108  func (w *typeWriter) error(msg string) {
   109  	if w.ctxt != nil {
   110  		panic(msg)
   111  	}
   112  	w.buf.WriteString("<" + msg + ">")
   113  }
   114  
   115  func (w *typeWriter) typ(typ Type) {
   116  	if w.seen[typ] {
   117  		w.error("cycle to " + goTypeName(typ))
   118  		return
   119  	}
   120  	w.seen[typ] = true
   121  	defer delete(w.seen, typ)
   122  
   123  	switch t := typ.(type) {
   124  	case nil:
   125  		w.error("nil")
   126  
   127  	case *Basic:
   128  		// exported basic types go into package unsafe
   129  		// (currently this is just unsafe.Pointer)
   130  		if isExported(t.name) {
   131  			if obj, _ := Unsafe.scope.Lookup(t.name).(*TypeName); obj != nil {
   132  				w.typeName(obj)
   133  				break
   134  			}
   135  		}
   136  		w.string(t.name)
   137  
   138  	case *Array:
   139  		w.byte('[')
   140  		w.string(strconv.FormatInt(t.len, 10))
   141  		w.byte(']')
   142  		w.typ(t.elem)
   143  
   144  	case *Slice:
   145  		w.string("[]")
   146  		w.typ(t.elem)
   147  
   148  	case *Struct:
   149  		w.string("struct{")
   150  		for i, f := range t.fields {
   151  			if i > 0 {
   152  				w.byte(';')
   153  			}
   154  
   155  			// If disambiguating one struct for another, look for the first unexported field.
   156  			// Do this first in case of nested structs; tag the first-outermost field.
   157  			pkgAnnotate := false
   158  			if w.qf == nil && w.pkgInfo && !isExported(f.name) {
   159  				// note for embedded types, type name is field name, and "string" etc are lower case hence unexported.
   160  				pkgAnnotate = true
   161  				w.pkgInfo = false // only tag once
   162  			}
   163  
   164  			// This doesn't do the right thing for embedded type
   165  			// aliases where we should print the alias name, not
   166  			// the aliased type (see go.dev/issue/44410).
   167  			if !f.embedded {
   168  				w.string(f.name)
   169  				w.byte(' ')
   170  			}
   171  			w.typ(f.typ)
   172  			if pkgAnnotate {
   173  				w.string(" /* package ")
   174  				w.string(f.pkg.Path())
   175  				w.string(" */ ")
   176  			}
   177  			if tag := t.Tag(i); tag != "" {
   178  				w.byte(' ')
   179  				// TODO(gri) If tag contains blanks, replacing them with '#'
   180  				//           in Context.TypeHash may produce another tag
   181  				//           accidentally.
   182  				w.string(strconv.Quote(tag))
   183  			}
   184  		}
   185  		w.byte('}')
   186  
   187  	case *Pointer:
   188  		w.byte('*')
   189  		w.typ(t.base)
   190  
   191  	case *Tuple:
   192  		w.tuple(t, false)
   193  
   194  	case *Signature:
   195  		w.string("func")
   196  		w.signature(t)
   197  
   198  	case *Union:
   199  		// Unions only appear as (syntactic) embedded elements
   200  		// in interfaces and syntactically cannot be empty.
   201  		if t.Len() == 0 {
   202  			w.error("empty union")
   203  			break
   204  		}
   205  		for i, t := range t.terms {
   206  			if i > 0 {
   207  				w.string(termSep)
   208  			}
   209  			if t.tilde {
   210  				w.byte('~')
   211  			}
   212  			w.typ(t.typ)
   213  		}
   214  
   215  	case *Interface:
   216  		if w.ctxt == nil {
   217  			if t == universeAny.Type() {
   218  				// When not hashing, we can try to improve type strings by writing "any"
   219  				// for a type that is pointer-identical to universeAny. This logic should
   220  				// be deprecated by more robust handling for aliases.
   221  				w.string("any")
   222  				break
   223  			}
   224  			if t == asNamed(universeComparable.Type()).underlying {
   225  				w.string("interface{comparable}")
   226  				break
   227  			}
   228  		}
   229  		if t.implicit {
   230  			if len(t.methods) == 0 && len(t.embeddeds) == 1 {
   231  				w.typ(t.embeddeds[0])
   232  				break
   233  			}
   234  			// Something's wrong with the implicit interface.
   235  			// Print it as such and continue.
   236  			w.string("/* implicit */ ")
   237  		}
   238  		w.string("interface{")
   239  		first := true
   240  		if w.ctxt != nil {
   241  			w.typeSet(t.typeSet())
   242  		} else {
   243  			for _, m := range t.methods {
   244  				if !first {
   245  					w.byte(';')
   246  				}
   247  				first = false
   248  				w.string(m.name)
   249  				w.signature(m.typ.(*Signature))
   250  			}
   251  			for _, typ := range t.embeddeds {
   252  				if !first {
   253  					w.byte(';')
   254  				}
   255  				first = false
   256  				w.typ(typ)
   257  			}
   258  		}
   259  		w.byte('}')
   260  
   261  	case *Map:
   262  		w.string("map[")
   263  		w.typ(t.key)
   264  		w.byte(']')
   265  		w.typ(t.elem)
   266  
   267  	case *Chan:
   268  		var s string
   269  		var parens bool
   270  		switch t.dir {
   271  		case SendRecv:
   272  			s = "chan "
   273  			// chan (<-chan T) requires parentheses
   274  			if c, _ := t.elem.(*Chan); c != nil && c.dir == RecvOnly {
   275  				parens = true
   276  			}
   277  		case SendOnly:
   278  			s = "chan<- "
   279  		case RecvOnly:
   280  			s = "<-chan "
   281  		default:
   282  			w.error("unknown channel direction")
   283  		}
   284  		w.string(s)
   285  		if parens {
   286  			w.byte('(')
   287  		}
   288  		w.typ(t.elem)
   289  		if parens {
   290  			w.byte(')')
   291  		}
   292  
   293  	case *Named:
   294  		// If hashing, write a unique prefix for t to represent its identity, since
   295  		// named type identity is pointer identity.
   296  		if w.ctxt != nil {
   297  			w.string(strconv.Itoa(w.ctxt.getID(t)))
   298  		}
   299  		w.typeName(t.obj) // when hashing written for readability of the hash only
   300  		if t.inst != nil {
   301  			// instantiated type
   302  			w.typeList(t.inst.targs.list())
   303  		} else if w.ctxt == nil && t.TypeParams().Len() != 0 { // For type hashing, don't need to format the TypeParams
   304  			// parameterized type
   305  			w.tParamList(t.TypeParams().list())
   306  		}
   307  
   308  	case *TypeParam:
   309  		if t.obj == nil {
   310  			w.error("unnamed type parameter")
   311  			break
   312  		}
   313  		if i := tparamIndex(w.tparams.list(), t); i >= 0 {
   314  			// The names of type parameters that are declared by the type being
   315  			// hashed are not part of the type identity. Replace them with a
   316  			// placeholder indicating their index.
   317  			w.string(fmt.Sprintf("$%d", i))
   318  		} else {
   319  			w.string(t.obj.name)
   320  			if w.tpSubscripts || w.ctxt != nil {
   321  				w.string(subscript(t.id))
   322  			}
   323  			// If the type parameter name is the same as a predeclared object
   324  			// (say int), point out where it is declared to avoid confusing
   325  			// error messages. This doesn't need to be super-elegant; we just
   326  			// need a clear indication that this is not a predeclared name.
   327  			if w.ctxt == nil && Universe.Lookup(t.obj.name) != nil {
   328  				if isTypes2 {
   329  					w.string(fmt.Sprintf(" /* with %s declared at %v */", t.obj.name, t.obj.Pos()))
   330  				} else {
   331  					// Can't print position information because
   332  					// we don't have a token.FileSet accessible.
   333  					w.string("/* type parameter */")
   334  				}
   335  			}
   336  		}
   337  
   338  	case *Alias:
   339  		w.typeName(t.obj)
   340  		if w.ctxt != nil {
   341  			// TODO(gri) do we need to print the alias type name, too?
   342  			w.typ(Unalias(t.obj.typ))
   343  		}
   344  
   345  	default:
   346  		// For externally defined implementations of Type.
   347  		// Note: In this case cycles won't be caught.
   348  		w.string(t.String())
   349  	}
   350  }
   351  
   352  // typeSet writes a canonical hash for an interface type set.
   353  func (w *typeWriter) typeSet(s *_TypeSet) {
   354  	assert(w.ctxt != nil)
   355  	first := true
   356  	for _, m := range s.methods {
   357  		if !first {
   358  			w.byte(';')
   359  		}
   360  		first = false
   361  		w.string(m.name)
   362  		w.signature(m.typ.(*Signature))
   363  	}
   364  	switch {
   365  	case s.terms.isAll():
   366  		// nothing to do
   367  	case s.terms.isEmpty():
   368  		w.string(s.terms.String())
   369  	default:
   370  		var termHashes []string
   371  		for _, term := range s.terms {
   372  			// terms are not canonically sorted, so we sort their hashes instead.
   373  			var buf bytes.Buffer
   374  			if term.tilde {
   375  				buf.WriteByte('~')
   376  			}
   377  			newTypeHasher(&buf, w.ctxt).typ(term.typ)
   378  			termHashes = append(termHashes, buf.String())
   379  		}
   380  		sort.Strings(termHashes)
   381  		if !first {
   382  			w.byte(';')
   383  		}
   384  		w.string(strings.Join(termHashes, "|"))
   385  	}
   386  }
   387  
   388  func (w *typeWriter) typeList(list []Type) {
   389  	w.byte('[')
   390  	for i, typ := range list {
   391  		if i > 0 {
   392  			w.byte(',')
   393  		}
   394  		w.typ(typ)
   395  	}
   396  	w.byte(']')
   397  }
   398  
   399  func (w *typeWriter) tParamList(list []*TypeParam) {
   400  	w.byte('[')
   401  	var prev Type
   402  	for i, tpar := range list {
   403  		// Determine the type parameter and its constraint.
   404  		// list is expected to hold type parameter names,
   405  		// but don't crash if that's not the case.
   406  		if tpar == nil {
   407  			w.error("nil type parameter")
   408  			continue
   409  		}
   410  		if i > 0 {
   411  			if tpar.bound != prev {
   412  				// bound changed - write previous one before advancing
   413  				w.byte(' ')
   414  				w.typ(prev)
   415  			}
   416  			w.byte(',')
   417  		}
   418  		prev = tpar.bound
   419  		w.typ(tpar)
   420  	}
   421  	if prev != nil {
   422  		w.byte(' ')
   423  		w.typ(prev)
   424  	}
   425  	w.byte(']')
   426  }
   427  
   428  func (w *typeWriter) typeName(obj *TypeName) {
   429  	w.string(packagePrefix(obj.pkg, w.qf))
   430  	w.string(obj.name)
   431  }
   432  
   433  func (w *typeWriter) tuple(tup *Tuple, variadic bool) {
   434  	w.byte('(')
   435  	if tup != nil {
   436  		for i, v := range tup.vars {
   437  			if i > 0 {
   438  				w.byte(',')
   439  			}
   440  			// parameter names are ignored for type identity and thus type hashes
   441  			if w.ctxt == nil && v.name != "" && w.paramNames {
   442  				w.string(v.name)
   443  				w.byte(' ')
   444  			}
   445  			typ := v.typ
   446  			if variadic && i == len(tup.vars)-1 {
   447  				if s, ok := typ.(*Slice); ok {
   448  					w.string("...")
   449  					typ = s.elem
   450  				} else {
   451  					// special case:
   452  					// append(s, "foo"...) leads to signature func([]byte, string...)
   453  					if t, _ := under(typ).(*Basic); t == nil || t.kind != String {
   454  						w.error("expected string type")
   455  						continue
   456  					}
   457  					w.typ(typ)
   458  					w.string("...")
   459  					continue
   460  				}
   461  			}
   462  			w.typ(typ)
   463  		}
   464  	}
   465  	w.byte(')')
   466  }
   467  
   468  func (w *typeWriter) signature(sig *Signature) {
   469  	if sig.TypeParams().Len() != 0 {
   470  		if w.ctxt != nil {
   471  			assert(w.tparams == nil)
   472  			w.tparams = sig.TypeParams()
   473  			defer func() {
   474  				w.tparams = nil
   475  			}()
   476  		}
   477  		w.tParamList(sig.TypeParams().list())
   478  	}
   479  
   480  	w.tuple(sig.params, sig.variadic)
   481  
   482  	n := sig.results.Len()
   483  	if n == 0 {
   484  		// no result
   485  		return
   486  	}
   487  
   488  	w.byte(' ')
   489  	if n == 1 && (w.ctxt != nil || sig.results.vars[0].name == "") {
   490  		// single unnamed result (if type hashing, name must be ignored)
   491  		w.typ(sig.results.vars[0].typ)
   492  		return
   493  	}
   494  
   495  	// multiple or named result(s)
   496  	w.tuple(sig.results, false)
   497  }
   498  
   499  // subscript returns the decimal (utf8) representation of x using subscript digits.
   500  func subscript(x uint64) string {
   501  	const w = len("₀") // all digits 0...9 have the same utf8 width
   502  	var buf [32 * w]byte
   503  	i := len(buf)
   504  	for {
   505  		i -= w
   506  		utf8.EncodeRune(buf[i:], '₀'+rune(x%10)) // '₀' == U+2080
   507  		x /= 10
   508  		if x == 0 {
   509  			break
   510  		}
   511  	}
   512  	return string(buf[i:])
   513  }
   514  

View as plain text