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  	"slices"
    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 == asNamed(universeComparable.Type()).underlying {
   218  				w.string("interface{comparable}")
   219  				break
   220  			}
   221  		}
   222  		if t.implicit {
   223  			if len(t.methods) == 0 && len(t.embeddeds) == 1 {
   224  				w.typ(t.embeddeds[0])
   225  				break
   226  			}
   227  			// Something's wrong with the implicit interface.
   228  			// Print it as such and continue.
   229  			w.string("/* implicit */ ")
   230  		}
   231  		w.string("interface{")
   232  		first := true
   233  		if w.ctxt != nil {
   234  			w.typeSet(t.typeSet())
   235  		} else {
   236  			for _, m := range t.methods {
   237  				if !first {
   238  					w.byte(';')
   239  				}
   240  				first = false
   241  				w.string(m.name)
   242  				w.signature(m.typ.(*Signature))
   243  			}
   244  			for _, typ := range t.embeddeds {
   245  				if !first {
   246  					w.byte(';')
   247  				}
   248  				first = false
   249  				w.typ(typ)
   250  			}
   251  		}
   252  		w.byte('}')
   253  
   254  	case *Map:
   255  		w.string("map[")
   256  		w.typ(t.key)
   257  		w.byte(']')
   258  		w.typ(t.elem)
   259  
   260  	case *Chan:
   261  		var s string
   262  		var parens bool
   263  		switch t.dir {
   264  		case SendRecv:
   265  			s = "chan "
   266  			// chan (<-chan T) requires parentheses
   267  			if c, _ := t.elem.(*Chan); c != nil && c.dir == RecvOnly {
   268  				parens = true
   269  			}
   270  		case SendOnly:
   271  			s = "chan<- "
   272  		case RecvOnly:
   273  			s = "<-chan "
   274  		default:
   275  			w.error("unknown channel direction")
   276  		}
   277  		w.string(s)
   278  		if parens {
   279  			w.byte('(')
   280  		}
   281  		w.typ(t.elem)
   282  		if parens {
   283  			w.byte(')')
   284  		}
   285  
   286  	case *Named:
   287  		// If hashing, write a unique prefix for t to represent its identity, since
   288  		// named type identity is pointer identity.
   289  		if w.ctxt != nil {
   290  			w.string(strconv.Itoa(w.ctxt.getID(t)))
   291  		}
   292  		w.typeName(t.obj) // when hashing written for readability of the hash only
   293  		if t.inst != nil {
   294  			// instantiated type
   295  			w.typeList(t.inst.targs.list())
   296  		} else if w.ctxt == nil && t.TypeParams().Len() != 0 { // For type hashing, don't need to format the TypeParams
   297  			// parameterized type
   298  			w.tParamList(t.TypeParams().list())
   299  		}
   300  
   301  	case *TypeParam:
   302  		if t.obj == nil {
   303  			w.error("unnamed type parameter")
   304  			break
   305  		}
   306  		if i := slices.Index(w.tparams.list(), t); i >= 0 {
   307  			// The names of type parameters that are declared by the type being
   308  			// hashed are not part of the type identity. Replace them with a
   309  			// placeholder indicating their index.
   310  			w.string(fmt.Sprintf("$%d", i))
   311  		} else {
   312  			w.string(t.obj.name)
   313  			if w.tpSubscripts || w.ctxt != nil {
   314  				w.string(subscript(t.id))
   315  			}
   316  			// If the type parameter name is the same as a predeclared object
   317  			// (say int), point out where it is declared to avoid confusing
   318  			// error messages. This doesn't need to be super-elegant; we just
   319  			// need a clear indication that this is not a predeclared name.
   320  			if w.ctxt == nil && Universe.Lookup(t.obj.name) != nil {
   321  				if isTypes2 {
   322  					w.string(fmt.Sprintf(" /* with %s declared at %v */", t.obj.name, t.obj.Pos()))
   323  				} else {
   324  					// Can't print position information because
   325  					// we don't have a token.FileSet accessible.
   326  					w.string("/* type parameter */")
   327  				}
   328  			}
   329  		}
   330  
   331  	case *Alias:
   332  		w.typeName(t.obj)
   333  		if list := t.targs.list(); len(list) != 0 {
   334  			// instantiated type
   335  			w.typeList(list)
   336  		} else if w.ctxt == nil && t.TypeParams().Len() != 0 { // For type hashing, don't need to format the TypeParams
   337  			// parameterized type
   338  			w.tParamList(t.TypeParams().list())
   339  		}
   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  		slices.Sort(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 slice, ok := typ.(*Slice); ok {
   448  					w.string("...")
   449  					w.typ(slice.elem)
   450  				} else {
   451  					// append(slice, str...) entails various special
   452  					// cases, especially in conjunction with generics.
   453  					// str may be:
   454  					// - a string,
   455  					// - a TypeParam whose typeset includes string, or
   456  					// - a named []byte slice type B resulting from
   457  					//   a client instantiating append([]byte, T) at T=B.
   458  					// For such cases we use the irregular notation
   459  					// func([]byte, T...), with the dots after the type.
   460  					w.typ(typ)
   461  					w.string("...")
   462  				}
   463  			} else {
   464  				w.typ(typ)
   465  			}
   466  		}
   467  	}
   468  	w.byte(')')
   469  }
   470  
   471  func (w *typeWriter) signature(sig *Signature) {
   472  	if sig.TypeParams().Len() != 0 {
   473  		if w.ctxt != nil {
   474  			assert(w.tparams == nil)
   475  			w.tparams = sig.TypeParams()
   476  			defer func() {
   477  				w.tparams = nil
   478  			}()
   479  		}
   480  		w.tParamList(sig.TypeParams().list())
   481  	}
   482  
   483  	w.tuple(sig.params, sig.variadic)
   484  
   485  	n := sig.results.Len()
   486  	if n == 0 {
   487  		// no result
   488  		return
   489  	}
   490  
   491  	w.byte(' ')
   492  	if n == 1 && (w.ctxt != nil || sig.results.vars[0].name == "") {
   493  		// single unnamed result (if type hashing, name must be ignored)
   494  		w.typ(sig.results.vars[0].typ)
   495  		return
   496  	}
   497  
   498  	// multiple or named result(s)
   499  	w.tuple(sig.results, false)
   500  }
   501  
   502  // subscript returns the decimal (utf8) representation of x using subscript digits.
   503  func subscript(x uint64) string {
   504  	const w = len("₀") // all digits 0...9 have the same utf8 width
   505  	var buf [32 * w]byte
   506  	i := len(buf)
   507  	for {
   508  		i -= w
   509  		utf8.EncodeRune(buf[i:], '₀'+rune(x%10)) // '₀' == U+2080
   510  		x /= 10
   511  		if x == 0 {
   512  			break
   513  		}
   514  	}
   515  	return string(buf[i:])
   516  }
   517  

View as plain text