Source file src/cmd/compile/internal/types/type.go

     1  // Copyright 2017 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 types
     6  
     7  import (
     8  	"cmd/compile/internal/base"
     9  	"cmd/internal/objabi"
    10  	"cmd/internal/src"
    11  	"fmt"
    12  	"go/constant"
    13  	"internal/types/errors"
    14  	"sync"
    15  )
    16  
    17  // Object represents an ir.Node, but without needing to import cmd/compile/internal/ir,
    18  // which would cause an import cycle. The uses in other packages must type assert
    19  // values of type Object to ir.Node or a more specific type.
    20  type Object interface {
    21  	Pos() src.XPos
    22  	Sym() *Sym
    23  	Type() *Type
    24  }
    25  
    26  //go:generate stringer -type Kind -trimprefix T type.go
    27  
    28  // Kind describes a kind of type.
    29  type Kind uint8
    30  
    31  const (
    32  	Txxx Kind = iota
    33  
    34  	TINT8
    35  	TUINT8
    36  	TINT16
    37  	TUINT16
    38  	TINT32
    39  	TUINT32
    40  	TINT64
    41  	TUINT64
    42  	TINT
    43  	TUINT
    44  	TUINTPTR
    45  
    46  	TCOMPLEX64
    47  	TCOMPLEX128
    48  
    49  	TFLOAT32
    50  	TFLOAT64
    51  
    52  	TBOOL
    53  
    54  	TPTR
    55  	TFUNC
    56  	TSLICE
    57  	TARRAY
    58  	TSTRUCT
    59  	TCHAN
    60  	TMAP
    61  	TINTER
    62  	TFORW
    63  	TANY
    64  	TSTRING
    65  	TUNSAFEPTR
    66  
    67  	// pseudo-types for literals
    68  	TIDEAL // untyped numeric constants
    69  	TNIL
    70  	TBLANK
    71  
    72  	// pseudo-types used temporarily only during frame layout (CalcSize())
    73  	TFUNCARGS
    74  	TCHANARGS
    75  
    76  	// SSA backend types
    77  	TSSA     // internal types used by SSA backend (flags, memory, etc.)
    78  	TTUPLE   // a pair of types, used by SSA backend
    79  	TRESULTS // multiple types; the result of calling a function or method, with a memory at the end.
    80  
    81  	NTYPE
    82  )
    83  
    84  // ChanDir is whether a channel can send, receive, or both.
    85  type ChanDir uint8
    86  
    87  func (c ChanDir) CanRecv() bool { return c&Crecv != 0 }
    88  func (c ChanDir) CanSend() bool { return c&Csend != 0 }
    89  
    90  const (
    91  	// types of channel
    92  	// must match ../../../../reflect/type.go:/ChanDir
    93  	Crecv ChanDir = 1 << 0
    94  	Csend ChanDir = 1 << 1
    95  	Cboth ChanDir = Crecv | Csend
    96  )
    97  
    98  // Types stores pointers to predeclared named types.
    99  //
   100  // It also stores pointers to several special types:
   101  //   - Types[TANY] is the placeholder "any" type recognized by SubstArgTypes.
   102  //   - Types[TBLANK] represents the blank variable's type.
   103  //   - Types[TINTER] is the canonical "interface{}" type.
   104  //   - Types[TNIL] represents the predeclared "nil" value's type.
   105  //   - Types[TUNSAFEPTR] is package unsafe's Pointer type.
   106  var Types [NTYPE]*Type
   107  
   108  var (
   109  	// Predeclared alias types. These are actually created as distinct
   110  	// defined types for better error messages, but are then specially
   111  	// treated as identical to their respective underlying types.
   112  	AnyType  *Type
   113  	ByteType *Type
   114  	RuneType *Type
   115  
   116  	// Predeclared error interface type.
   117  	ErrorType *Type
   118  	// Predeclared comparable interface type.
   119  	ComparableType *Type
   120  
   121  	// Types to represent untyped string and boolean constants.
   122  	UntypedString = newType(TSTRING)
   123  	UntypedBool   = newType(TBOOL)
   124  
   125  	// Types to represent untyped numeric constants.
   126  	UntypedInt     = newType(TIDEAL)
   127  	UntypedRune    = newType(TIDEAL)
   128  	UntypedFloat   = newType(TIDEAL)
   129  	UntypedComplex = newType(TIDEAL)
   130  )
   131  
   132  // UntypedTypes maps from a constant.Kind to its untyped Type
   133  // representation.
   134  var UntypedTypes = [...]*Type{
   135  	constant.Bool:    UntypedBool,
   136  	constant.String:  UntypedString,
   137  	constant.Int:     UntypedInt,
   138  	constant.Float:   UntypedFloat,
   139  	constant.Complex: UntypedComplex,
   140  }
   141  
   142  // DefaultKinds maps from a constant.Kind to its default Kind.
   143  var DefaultKinds = [...]Kind{
   144  	constant.Bool:    TBOOL,
   145  	constant.String:  TSTRING,
   146  	constant.Int:     TINT,
   147  	constant.Float:   TFLOAT64,
   148  	constant.Complex: TCOMPLEX128,
   149  }
   150  
   151  // A Type represents a Go type.
   152  //
   153  // There may be multiple unnamed types with identical structure. However, there must
   154  // be a unique Type object for each unique named (defined) type. After noding, a
   155  // package-level type can be looked up by building its unique symbol sym (sym =
   156  // package.Lookup(name)) and checking sym.Def. If sym.Def is non-nil, the type
   157  // already exists at package scope and is available at sym.Def.(*ir.Name).Type().
   158  // Local types (which may have the same name as a package-level type) are
   159  // distinguished by their vargen, which is embedded in their symbol name.
   160  type Type struct {
   161  	// extra contains extra etype-specific fields.
   162  	// As an optimization, those etype-specific structs which contain exactly
   163  	// one pointer-shaped field are stored as values rather than pointers when possible.
   164  	//
   165  	// TMAP: *Map
   166  	// TFORW: *Forward
   167  	// TFUNC: *Func
   168  	// TSTRUCT: *Struct
   169  	// TINTER: *Interface
   170  	// TFUNCARGS: FuncArgs
   171  	// TCHANARGS: ChanArgs
   172  	// TCHAN: *Chan
   173  	// TPTR: Ptr
   174  	// TARRAY: *Array
   175  	// TSLICE: Slice
   176  	// TSSA: string
   177  	extra interface{}
   178  
   179  	// width is the width of this Type in bytes.
   180  	width int64 // valid if Align > 0
   181  
   182  	// list of base methods (excluding embedding)
   183  	methods fields
   184  	// list of all methods (including embedding)
   185  	allMethods fields
   186  
   187  	// canonical OTYPE node for a named type (should be an ir.Name node with same sym)
   188  	obj Object
   189  	// the underlying type (type literal or predeclared type) for a defined type
   190  	underlying *Type
   191  
   192  	// Cache of composite types, with this type being the element type.
   193  	cache struct {
   194  		ptr   *Type // *T, or nil
   195  		slice *Type // []T, or nil
   196  	}
   197  
   198  	kind  Kind  // kind of type
   199  	align uint8 // the required alignment of this type, in bytes (0 means Width and Align have not yet been computed)
   200  
   201  	intRegs, floatRegs uint8 // registers needed for ABIInternal
   202  
   203  	flags bitset8
   204  	alg   AlgKind // valid if Align > 0
   205  
   206  	// size of prefix of object that contains all pointers. valid if Align > 0.
   207  	// Note that for pointers, this is always PtrSize even if the element type
   208  	// is NotInHeap. See size.go:PtrDataSize for details.
   209  	ptrBytes int64
   210  
   211  	// For defined (named) generic types, a pointer to the list of type params
   212  	// (in order) of this type that need to be instantiated. For instantiated
   213  	// generic types, this is the targs used to instantiate them. These targs
   214  	// may be typeparams (for re-instantiated types such as Value[T2]) or
   215  	// concrete types (for fully instantiated types such as Value[int]).
   216  	// rparams is only set for named types that are generic or are fully
   217  	// instantiated from a generic type, and is otherwise set to nil.
   218  	// TODO(danscales): choose a better name.
   219  	rparams *[]*Type
   220  }
   221  
   222  // Registers returns the number of integer and floating-point
   223  // registers required to represent a parameter of this type under the
   224  // ABIInternal calling conventions.
   225  //
   226  // If t must be passed by memory, Registers returns (math.MaxUint8,
   227  // math.MaxUint8).
   228  func (t *Type) Registers() (uint8, uint8) {
   229  	CalcSize(t)
   230  	return t.intRegs, t.floatRegs
   231  }
   232  
   233  func (*Type) CanBeAnSSAAux() {}
   234  
   235  const (
   236  	typeNotInHeap  = 1 << iota // type cannot be heap allocated
   237  	typeNoalg                  // suppress hash and eq algorithm generation
   238  	typeDeferwidth             // width computation has been deferred and type is on deferredTypeStack
   239  	typeRecur
   240  	typeIsShape  // represents a set of closely related types, for generics
   241  	typeHasShape // there is a shape somewhere in the type
   242  )
   243  
   244  func (t *Type) NotInHeap() bool  { return t.flags&typeNotInHeap != 0 }
   245  func (t *Type) Noalg() bool      { return t.flags&typeNoalg != 0 }
   246  func (t *Type) Deferwidth() bool { return t.flags&typeDeferwidth != 0 }
   247  func (t *Type) Recur() bool      { return t.flags&typeRecur != 0 }
   248  func (t *Type) IsShape() bool    { return t.flags&typeIsShape != 0 }
   249  func (t *Type) HasShape() bool   { return t.flags&typeHasShape != 0 }
   250  
   251  func (t *Type) SetNotInHeap(b bool)  { t.flags.set(typeNotInHeap, b) }
   252  func (t *Type) SetNoalg(b bool)      { t.flags.set(typeNoalg, b) }
   253  func (t *Type) SetDeferwidth(b bool) { t.flags.set(typeDeferwidth, b) }
   254  func (t *Type) SetRecur(b bool)      { t.flags.set(typeRecur, b) }
   255  
   256  // Should always do SetHasShape(true) when doing SetIsShape(true).
   257  func (t *Type) SetIsShape(b bool)  { t.flags.set(typeIsShape, b) }
   258  func (t *Type) SetHasShape(b bool) { t.flags.set(typeHasShape, b) }
   259  
   260  // Kind returns the kind of type t.
   261  func (t *Type) Kind() Kind { return t.kind }
   262  
   263  // Sym returns the name of type t.
   264  func (t *Type) Sym() *Sym {
   265  	if t.obj != nil {
   266  		return t.obj.Sym()
   267  	}
   268  	return nil
   269  }
   270  
   271  // Underlying returns the underlying type of type t.
   272  func (t *Type) Underlying() *Type { return t.underlying }
   273  
   274  // Pos returns a position associated with t, if any.
   275  // This should only be used for diagnostics.
   276  func (t *Type) Pos() src.XPos {
   277  	if t.obj != nil {
   278  		return t.obj.Pos()
   279  	}
   280  	return src.NoXPos
   281  }
   282  
   283  func (t *Type) RParams() []*Type {
   284  	if t.rparams == nil {
   285  		return nil
   286  	}
   287  	return *t.rparams
   288  }
   289  
   290  func (t *Type) SetRParams(rparams []*Type) {
   291  	if len(rparams) == 0 {
   292  		base.Fatalf("Setting nil or zero-length rparams")
   293  	}
   294  	t.rparams = &rparams
   295  	// HasShape should be set if any type argument is or has a shape type.
   296  	for _, rparam := range rparams {
   297  		if rparam.HasShape() {
   298  			t.SetHasShape(true)
   299  			break
   300  		}
   301  	}
   302  }
   303  
   304  // IsFullyInstantiated reports whether t is a fully instantiated generic type; i.e. an
   305  // instantiated generic type where all type arguments are non-generic or fully
   306  // instantiated generic types.
   307  func (t *Type) IsFullyInstantiated() bool {
   308  	return len(t.RParams()) > 0
   309  }
   310  
   311  // Map contains Type fields specific to maps.
   312  type Map struct {
   313  	Key  *Type // Key type
   314  	Elem *Type // Val (elem) type
   315  
   316  	Bucket *Type // internal struct type representing a hash bucket
   317  }
   318  
   319  // MapType returns t's extra map-specific fields.
   320  func (t *Type) MapType() *Map {
   321  	t.wantEtype(TMAP)
   322  	return t.extra.(*Map)
   323  }
   324  
   325  // Forward contains Type fields specific to forward types.
   326  type Forward struct {
   327  	Copyto      []*Type  // where to copy the eventual value to
   328  	Embedlineno src.XPos // first use of this type as an embedded type
   329  }
   330  
   331  // forwardType returns t's extra forward-type-specific fields.
   332  func (t *Type) forwardType() *Forward {
   333  	t.wantEtype(TFORW)
   334  	return t.extra.(*Forward)
   335  }
   336  
   337  // Func contains Type fields specific to func types.
   338  type Func struct {
   339  	allParams []*Field // slice of all parameters, in receiver/params/results order
   340  
   341  	startParams  int // index of the start of the (regular) parameters section
   342  	startResults int // index of the start of the results section
   343  
   344  	resultsTuple *Type // struct-like type representing multi-value results
   345  
   346  	// Argwid is the total width of the function receiver, params, and results.
   347  	// It gets calculated via a temporary TFUNCARGS type.
   348  	// Note that TFUNC's Width is Widthptr.
   349  	Argwid int64
   350  }
   351  
   352  func (ft *Func) recvs() []*Field         { return ft.allParams[:ft.startParams] }
   353  func (ft *Func) params() []*Field        { return ft.allParams[ft.startParams:ft.startResults] }
   354  func (ft *Func) results() []*Field       { return ft.allParams[ft.startResults:] }
   355  func (ft *Func) recvParams() []*Field    { return ft.allParams[:ft.startResults] }
   356  func (ft *Func) paramsResults() []*Field { return ft.allParams[ft.startParams:] }
   357  
   358  // funcType returns t's extra func-specific fields.
   359  func (t *Type) funcType() *Func {
   360  	t.wantEtype(TFUNC)
   361  	return t.extra.(*Func)
   362  }
   363  
   364  // StructType contains Type fields specific to struct types.
   365  type Struct struct {
   366  	fields fields
   367  
   368  	// Maps have three associated internal structs (see struct MapType).
   369  	// Map links such structs back to their map type.
   370  	Map *Type
   371  
   372  	ParamTuple bool // whether this struct is actually a tuple of signature parameters
   373  }
   374  
   375  // StructType returns t's extra struct-specific fields.
   376  func (t *Type) StructType() *Struct {
   377  	t.wantEtype(TSTRUCT)
   378  	return t.extra.(*Struct)
   379  }
   380  
   381  // Interface contains Type fields specific to interface types.
   382  type Interface struct {
   383  }
   384  
   385  // Ptr contains Type fields specific to pointer types.
   386  type Ptr struct {
   387  	Elem *Type // element type
   388  }
   389  
   390  // ChanArgs contains Type fields specific to TCHANARGS types.
   391  type ChanArgs struct {
   392  	T *Type // reference to a chan type whose elements need a width check
   393  }
   394  
   395  // // FuncArgs contains Type fields specific to TFUNCARGS types.
   396  type FuncArgs struct {
   397  	T *Type // reference to a func type whose elements need a width check
   398  }
   399  
   400  // Chan contains Type fields specific to channel types.
   401  type Chan struct {
   402  	Elem *Type   // element type
   403  	Dir  ChanDir // channel direction
   404  }
   405  
   406  // chanType returns t's extra channel-specific fields.
   407  func (t *Type) chanType() *Chan {
   408  	t.wantEtype(TCHAN)
   409  	return t.extra.(*Chan)
   410  }
   411  
   412  type Tuple struct {
   413  	first  *Type
   414  	second *Type
   415  	// Any tuple with a memory type must put that memory type second.
   416  }
   417  
   418  // Results are the output from calls that will be late-expanded.
   419  type Results struct {
   420  	Types []*Type // Last element is memory output from call.
   421  }
   422  
   423  // Array contains Type fields specific to array types.
   424  type Array struct {
   425  	Elem  *Type // element type
   426  	Bound int64 // number of elements; <0 if unknown yet
   427  }
   428  
   429  // Slice contains Type fields specific to slice types.
   430  type Slice struct {
   431  	Elem *Type // element type
   432  }
   433  
   434  // A Field is a (Sym, Type) pairing along with some other information, and,
   435  // depending on the context, is used to represent:
   436  //   - a field in a struct
   437  //   - a method in an interface or associated with a named type
   438  //   - a function parameter
   439  type Field struct {
   440  	flags bitset8
   441  
   442  	Embedded uint8 // embedded field
   443  
   444  	Pos src.XPos
   445  
   446  	// Name of field/method/parameter. Can be nil for interface fields embedded
   447  	// in interfaces and unnamed parameters.
   448  	Sym  *Sym
   449  	Type *Type  // field type
   450  	Note string // literal string annotation
   451  
   452  	// For fields that represent function parameters, Nname points to the
   453  	// associated ONAME Node. For fields that represent methods, Nname points to
   454  	// the function name node.
   455  	Nname Object
   456  
   457  	// Offset in bytes of this field or method within its enclosing struct
   458  	// or interface Type. For parameters, this is BADWIDTH.
   459  	Offset int64
   460  }
   461  
   462  const (
   463  	fieldIsDDD = 1 << iota // field is ... argument
   464  	fieldNointerface
   465  )
   466  
   467  func (f *Field) IsDDD() bool       { return f.flags&fieldIsDDD != 0 }
   468  func (f *Field) Nointerface() bool { return f.flags&fieldNointerface != 0 }
   469  
   470  func (f *Field) SetIsDDD(b bool)       { f.flags.set(fieldIsDDD, b) }
   471  func (f *Field) SetNointerface(b bool) { f.flags.set(fieldNointerface, b) }
   472  
   473  // End returns the offset of the first byte immediately after this field.
   474  func (f *Field) End() int64 {
   475  	return f.Offset + f.Type.width
   476  }
   477  
   478  // IsMethod reports whether f represents a method rather than a struct field.
   479  func (f *Field) IsMethod() bool {
   480  	return f.Type.kind == TFUNC && f.Type.Recv() != nil
   481  }
   482  
   483  // fields is a pointer to a slice of *Field.
   484  // This saves space in Types that do not have fields or methods
   485  // compared to a simple slice of *Field.
   486  type fields struct {
   487  	s *[]*Field
   488  }
   489  
   490  // Slice returns the entries in f as a slice.
   491  // Changes to the slice entries will be reflected in f.
   492  func (f *fields) Slice() []*Field {
   493  	if f.s == nil {
   494  		return nil
   495  	}
   496  	return *f.s
   497  }
   498  
   499  // Set sets f to a slice.
   500  // This takes ownership of the slice.
   501  func (f *fields) Set(s []*Field) {
   502  	if len(s) == 0 {
   503  		f.s = nil
   504  	} else {
   505  		// Copy s and take address of t rather than s to avoid
   506  		// allocation in the case where len(s) == 0.
   507  		t := s
   508  		f.s = &t
   509  	}
   510  }
   511  
   512  // newType returns a new Type of the specified kind.
   513  func newType(et Kind) *Type {
   514  	t := &Type{
   515  		kind:  et,
   516  		width: BADWIDTH,
   517  	}
   518  	t.underlying = t
   519  	// TODO(josharian): lazily initialize some of these?
   520  	switch t.kind {
   521  	case TMAP:
   522  		t.extra = new(Map)
   523  	case TFORW:
   524  		t.extra = new(Forward)
   525  	case TFUNC:
   526  		t.extra = new(Func)
   527  	case TSTRUCT:
   528  		t.extra = new(Struct)
   529  	case TINTER:
   530  		t.extra = new(Interface)
   531  	case TPTR:
   532  		t.extra = Ptr{}
   533  	case TCHANARGS:
   534  		t.extra = ChanArgs{}
   535  	case TFUNCARGS:
   536  		t.extra = FuncArgs{}
   537  	case TCHAN:
   538  		t.extra = new(Chan)
   539  	case TTUPLE:
   540  		t.extra = new(Tuple)
   541  	case TRESULTS:
   542  		t.extra = new(Results)
   543  	}
   544  	return t
   545  }
   546  
   547  // NewArray returns a new fixed-length array Type.
   548  func NewArray(elem *Type, bound int64) *Type {
   549  	if bound < 0 {
   550  		base.Fatalf("NewArray: invalid bound %v", bound)
   551  	}
   552  	t := newType(TARRAY)
   553  	t.extra = &Array{Elem: elem, Bound: bound}
   554  	if elem.HasShape() {
   555  		t.SetHasShape(true)
   556  	}
   557  	if elem.NotInHeap() {
   558  		t.SetNotInHeap(true)
   559  	}
   560  	return t
   561  }
   562  
   563  // NewSlice returns the slice Type with element type elem.
   564  func NewSlice(elem *Type) *Type {
   565  	if t := elem.cache.slice; t != nil {
   566  		if t.Elem() != elem {
   567  			base.Fatalf("elem mismatch")
   568  		}
   569  		if elem.HasShape() != t.HasShape() {
   570  			base.Fatalf("Incorrect HasShape flag for cached slice type")
   571  		}
   572  		return t
   573  	}
   574  
   575  	t := newType(TSLICE)
   576  	t.extra = Slice{Elem: elem}
   577  	elem.cache.slice = t
   578  	if elem.HasShape() {
   579  		t.SetHasShape(true)
   580  	}
   581  	return t
   582  }
   583  
   584  // NewChan returns a new chan Type with direction dir.
   585  func NewChan(elem *Type, dir ChanDir) *Type {
   586  	t := newType(TCHAN)
   587  	ct := t.chanType()
   588  	ct.Elem = elem
   589  	ct.Dir = dir
   590  	if elem.HasShape() {
   591  		t.SetHasShape(true)
   592  	}
   593  	return t
   594  }
   595  
   596  func NewTuple(t1, t2 *Type) *Type {
   597  	t := newType(TTUPLE)
   598  	t.extra.(*Tuple).first = t1
   599  	t.extra.(*Tuple).second = t2
   600  	if t1.HasShape() || t2.HasShape() {
   601  		t.SetHasShape(true)
   602  	}
   603  	return t
   604  }
   605  
   606  func newResults(types []*Type) *Type {
   607  	t := newType(TRESULTS)
   608  	t.extra.(*Results).Types = types
   609  	return t
   610  }
   611  
   612  func NewResults(types []*Type) *Type {
   613  	if len(types) == 1 && types[0] == TypeMem {
   614  		return TypeResultMem
   615  	}
   616  	return newResults(types)
   617  }
   618  
   619  func newSSA(name string) *Type {
   620  	t := newType(TSSA)
   621  	t.extra = name
   622  	return t
   623  }
   624  
   625  // NewMap returns a new map Type with key type k and element (aka value) type v.
   626  func NewMap(k, v *Type) *Type {
   627  	t := newType(TMAP)
   628  	mt := t.MapType()
   629  	mt.Key = k
   630  	mt.Elem = v
   631  	if k.HasShape() || v.HasShape() {
   632  		t.SetHasShape(true)
   633  	}
   634  	return t
   635  }
   636  
   637  // NewPtrCacheEnabled controls whether *T Types are cached in T.
   638  // Caching is disabled just before starting the backend.
   639  // This allows the backend to run concurrently.
   640  var NewPtrCacheEnabled = true
   641  
   642  // NewPtr returns the pointer type pointing to t.
   643  func NewPtr(elem *Type) *Type {
   644  	if elem == nil {
   645  		base.Fatalf("NewPtr: pointer to elem Type is nil")
   646  	}
   647  
   648  	if t := elem.cache.ptr; t != nil {
   649  		if t.Elem() != elem {
   650  			base.Fatalf("NewPtr: elem mismatch")
   651  		}
   652  		if elem.HasShape() != t.HasShape() {
   653  			base.Fatalf("Incorrect HasShape flag for cached pointer type")
   654  		}
   655  		return t
   656  	}
   657  
   658  	t := newType(TPTR)
   659  	t.extra = Ptr{Elem: elem}
   660  	t.width = int64(PtrSize)
   661  	t.align = uint8(PtrSize)
   662  	t.intRegs = 1
   663  	if NewPtrCacheEnabled {
   664  		elem.cache.ptr = t
   665  	}
   666  	if elem.HasShape() {
   667  		t.SetHasShape(true)
   668  	}
   669  	t.alg = AMEM
   670  	if elem.Noalg() {
   671  		t.SetNoalg(true)
   672  		t.alg = ANOALG
   673  	}
   674  	// Note: we can't check elem.NotInHeap here because it might
   675  	// not be set yet. See size.go:PtrDataSize.
   676  	t.ptrBytes = int64(PtrSize)
   677  	return t
   678  }
   679  
   680  // NewChanArgs returns a new TCHANARGS type for channel type c.
   681  func NewChanArgs(c *Type) *Type {
   682  	t := newType(TCHANARGS)
   683  	t.extra = ChanArgs{T: c}
   684  	return t
   685  }
   686  
   687  // NewFuncArgs returns a new TFUNCARGS type for func type f.
   688  func NewFuncArgs(f *Type) *Type {
   689  	t := newType(TFUNCARGS)
   690  	t.extra = FuncArgs{T: f}
   691  	return t
   692  }
   693  
   694  func NewField(pos src.XPos, sym *Sym, typ *Type) *Field {
   695  	f := &Field{
   696  		Pos:    pos,
   697  		Sym:    sym,
   698  		Type:   typ,
   699  		Offset: BADWIDTH,
   700  	}
   701  	if typ == nil {
   702  		base.Fatalf("typ is nil")
   703  	}
   704  	return f
   705  }
   706  
   707  // SubstAny walks t, replacing instances of "any" with successive
   708  // elements removed from types.  It returns the substituted type.
   709  func SubstAny(t *Type, types *[]*Type) *Type {
   710  	if t == nil {
   711  		return nil
   712  	}
   713  
   714  	switch t.kind {
   715  	default:
   716  		// Leave the type unchanged.
   717  
   718  	case TANY:
   719  		if len(*types) == 0 {
   720  			base.Fatalf("SubstArgTypes: not enough argument types")
   721  		}
   722  		t = (*types)[0]
   723  		*types = (*types)[1:]
   724  
   725  	case TPTR:
   726  		elem := SubstAny(t.Elem(), types)
   727  		if elem != t.Elem() {
   728  			t = t.copy()
   729  			t.extra = Ptr{Elem: elem}
   730  		}
   731  
   732  	case TARRAY:
   733  		elem := SubstAny(t.Elem(), types)
   734  		if elem != t.Elem() {
   735  			t = t.copy()
   736  			t.extra.(*Array).Elem = elem
   737  		}
   738  
   739  	case TSLICE:
   740  		elem := SubstAny(t.Elem(), types)
   741  		if elem != t.Elem() {
   742  			t = t.copy()
   743  			t.extra = Slice{Elem: elem}
   744  		}
   745  
   746  	case TCHAN:
   747  		elem := SubstAny(t.Elem(), types)
   748  		if elem != t.Elem() {
   749  			t = t.copy()
   750  			t.extra.(*Chan).Elem = elem
   751  		}
   752  
   753  	case TMAP:
   754  		key := SubstAny(t.Key(), types)
   755  		elem := SubstAny(t.Elem(), types)
   756  		if key != t.Key() || elem != t.Elem() {
   757  			t = t.copy()
   758  			t.extra.(*Map).Key = key
   759  			t.extra.(*Map).Elem = elem
   760  		}
   761  
   762  	case TFUNC:
   763  		ft := t.funcType()
   764  		allParams := substFields(ft.allParams, types)
   765  
   766  		t = t.copy()
   767  		ft = t.funcType()
   768  		ft.allParams = allParams
   769  
   770  		rt := ft.resultsTuple
   771  		rt = rt.copy()
   772  		ft.resultsTuple = rt
   773  		rt.setFields(t.Results())
   774  
   775  	case TSTRUCT:
   776  		// Make a copy of all fields, including ones whose type does not change.
   777  		// This prevents aliasing across functions, which can lead to later
   778  		// fields getting their Offset incorrectly overwritten.
   779  		nfs := substFields(t.Fields(), types)
   780  		t = t.copy()
   781  		t.setFields(nfs)
   782  	}
   783  
   784  	return t
   785  }
   786  
   787  func substFields(fields []*Field, types *[]*Type) []*Field {
   788  	nfs := make([]*Field, len(fields))
   789  	for i, f := range fields {
   790  		nft := SubstAny(f.Type, types)
   791  		nfs[i] = f.Copy()
   792  		nfs[i].Type = nft
   793  	}
   794  	return nfs
   795  }
   796  
   797  // copy returns a shallow copy of the Type.
   798  func (t *Type) copy() *Type {
   799  	if t == nil {
   800  		return nil
   801  	}
   802  	nt := *t
   803  	// copy any *T Extra fields, to avoid aliasing
   804  	switch t.kind {
   805  	case TMAP:
   806  		x := *t.extra.(*Map)
   807  		nt.extra = &x
   808  	case TFORW:
   809  		x := *t.extra.(*Forward)
   810  		nt.extra = &x
   811  	case TFUNC:
   812  		x := *t.extra.(*Func)
   813  		nt.extra = &x
   814  	case TSTRUCT:
   815  		x := *t.extra.(*Struct)
   816  		nt.extra = &x
   817  	case TINTER:
   818  		x := *t.extra.(*Interface)
   819  		nt.extra = &x
   820  	case TCHAN:
   821  		x := *t.extra.(*Chan)
   822  		nt.extra = &x
   823  	case TARRAY:
   824  		x := *t.extra.(*Array)
   825  		nt.extra = &x
   826  	case TTUPLE, TSSA, TRESULTS:
   827  		base.Fatalf("ssa types cannot be copied")
   828  	}
   829  	// TODO(mdempsky): Find out why this is necessary and explain.
   830  	if t.underlying == t {
   831  		nt.underlying = &nt
   832  	}
   833  	return &nt
   834  }
   835  
   836  func (f *Field) Copy() *Field {
   837  	nf := *f
   838  	return &nf
   839  }
   840  
   841  func (t *Type) wantEtype(et Kind) {
   842  	if t.kind != et {
   843  		base.Fatalf("want %v, but have %v", et, t)
   844  	}
   845  }
   846  
   847  // ResultsTuple returns the result type of signature type t as a tuple.
   848  // This can be used as the type of multi-valued call expressions.
   849  func (t *Type) ResultsTuple() *Type { return t.funcType().resultsTuple }
   850  
   851  // Recvs returns a slice of receiver parameters of signature type t.
   852  // The returned slice always has length 0 or 1.
   853  func (t *Type) Recvs() []*Field { return t.funcType().recvs() }
   854  
   855  // Params returns a slice of regular parameters of signature type t.
   856  func (t *Type) Params() []*Field { return t.funcType().params() }
   857  
   858  // Results returns a slice of result parameters of signature type t.
   859  func (t *Type) Results() []*Field { return t.funcType().results() }
   860  
   861  // RecvParamsResults returns a slice containing all of the
   862  // signature's parameters in receiver (if any), (normal) parameters,
   863  // and then results.
   864  func (t *Type) RecvParamsResults() []*Field { return t.funcType().allParams }
   865  
   866  // RecvParams returns a slice containing the signature's receiver (if
   867  // any) followed by its (normal) parameters.
   868  func (t *Type) RecvParams() []*Field { return t.funcType().recvParams() }
   869  
   870  // ParamsResults returns a slice containing the signature's (normal)
   871  // parameters followed by its results.
   872  func (t *Type) ParamsResults() []*Field { return t.funcType().paramsResults() }
   873  
   874  func (t *Type) NumRecvs() int   { return len(t.Recvs()) }
   875  func (t *Type) NumParams() int  { return len(t.Params()) }
   876  func (t *Type) NumResults() int { return len(t.Results()) }
   877  
   878  // IsVariadic reports whether function type t is variadic.
   879  func (t *Type) IsVariadic() bool {
   880  	n := t.NumParams()
   881  	return n > 0 && t.Param(n-1).IsDDD()
   882  }
   883  
   884  // Recv returns the receiver of function type t, if any.
   885  func (t *Type) Recv() *Field {
   886  	if s := t.Recvs(); len(s) == 1 {
   887  		return s[0]
   888  	}
   889  	return nil
   890  }
   891  
   892  // Param returns the i'th parameter of signature type t.
   893  func (t *Type) Param(i int) *Field { return t.Params()[i] }
   894  
   895  // Result returns the i'th result of signature type t.
   896  func (t *Type) Result(i int) *Field { return t.Results()[i] }
   897  
   898  // Key returns the key type of map type t.
   899  func (t *Type) Key() *Type {
   900  	t.wantEtype(TMAP)
   901  	return t.extra.(*Map).Key
   902  }
   903  
   904  // Elem returns the type of elements of t.
   905  // Usable with pointers, channels, arrays, slices, and maps.
   906  func (t *Type) Elem() *Type {
   907  	switch t.kind {
   908  	case TPTR:
   909  		return t.extra.(Ptr).Elem
   910  	case TARRAY:
   911  		return t.extra.(*Array).Elem
   912  	case TSLICE:
   913  		return t.extra.(Slice).Elem
   914  	case TCHAN:
   915  		return t.extra.(*Chan).Elem
   916  	case TMAP:
   917  		return t.extra.(*Map).Elem
   918  	}
   919  	base.Fatalf("Type.Elem %s", t.kind)
   920  	return nil
   921  }
   922  
   923  // ChanArgs returns the channel type for TCHANARGS type t.
   924  func (t *Type) ChanArgs() *Type {
   925  	t.wantEtype(TCHANARGS)
   926  	return t.extra.(ChanArgs).T
   927  }
   928  
   929  // FuncArgs returns the func type for TFUNCARGS type t.
   930  func (t *Type) FuncArgs() *Type {
   931  	t.wantEtype(TFUNCARGS)
   932  	return t.extra.(FuncArgs).T
   933  }
   934  
   935  // IsFuncArgStruct reports whether t is a struct representing function parameters or results.
   936  func (t *Type) IsFuncArgStruct() bool {
   937  	return t.kind == TSTRUCT && t.extra.(*Struct).ParamTuple
   938  }
   939  
   940  // Methods returns a pointer to the base methods (excluding embedding) for type t.
   941  // These can either be concrete methods (for non-interface types) or interface
   942  // methods (for interface types).
   943  func (t *Type) Methods() []*Field {
   944  	return t.methods.Slice()
   945  }
   946  
   947  // AllMethods returns a pointer to all the methods (including embedding) for type t.
   948  // For an interface type, this is the set of methods that are typically iterated
   949  // over. For non-interface types, AllMethods() only returns a valid result after
   950  // CalcMethods() has been called at least once.
   951  func (t *Type) AllMethods() []*Field {
   952  	if t.kind == TINTER {
   953  		// Calculate the full method set of an interface type on the fly
   954  		// now, if not done yet.
   955  		CalcSize(t)
   956  	}
   957  	return t.allMethods.Slice()
   958  }
   959  
   960  // SetMethods sets the direct method set for type t (i.e., *not*
   961  // including promoted methods from embedded types).
   962  func (t *Type) SetMethods(fs []*Field) {
   963  	t.methods.Set(fs)
   964  }
   965  
   966  // SetAllMethods sets the set of all methods for type t (i.e.,
   967  // including promoted methods from embedded types).
   968  func (t *Type) SetAllMethods(fs []*Field) {
   969  	t.allMethods.Set(fs)
   970  }
   971  
   972  // fields returns the fields of struct type t.
   973  func (t *Type) fields() *fields {
   974  	t.wantEtype(TSTRUCT)
   975  	return &t.extra.(*Struct).fields
   976  }
   977  
   978  // Field returns the i'th field of struct type t.
   979  func (t *Type) Field(i int) *Field { return t.Fields()[i] }
   980  
   981  // Fields returns a slice of containing all fields of
   982  // a struct type t.
   983  func (t *Type) Fields() []*Field { return t.fields().Slice() }
   984  
   985  // setFields sets struct type t's fields to fields.
   986  func (t *Type) setFields(fields []*Field) {
   987  	// If we've calculated the width of t before,
   988  	// then some other type such as a function signature
   989  	// might now have the wrong type.
   990  	// Rather than try to track and invalidate those,
   991  	// enforce that SetFields cannot be called once
   992  	// t's width has been calculated.
   993  	if t.widthCalculated() {
   994  		base.Fatalf("SetFields of %v: width previously calculated", t)
   995  	}
   996  	t.wantEtype(TSTRUCT)
   997  	t.fields().Set(fields)
   998  }
   999  
  1000  // SetInterface sets the base methods of an interface type t.
  1001  func (t *Type) SetInterface(methods []*Field) {
  1002  	t.wantEtype(TINTER)
  1003  	t.methods.Set(methods)
  1004  }
  1005  
  1006  // ArgWidth returns the total aligned argument size for a function.
  1007  // It includes the receiver, parameters, and results.
  1008  func (t *Type) ArgWidth() int64 {
  1009  	t.wantEtype(TFUNC)
  1010  	return t.extra.(*Func).Argwid
  1011  }
  1012  
  1013  func (t *Type) Size() int64 {
  1014  	if t.kind == TSSA {
  1015  		if t == TypeInt128 {
  1016  			return 16
  1017  		}
  1018  		return 0
  1019  	}
  1020  	CalcSize(t)
  1021  	return t.width
  1022  }
  1023  
  1024  func (t *Type) Alignment() int64 {
  1025  	CalcSize(t)
  1026  	return int64(t.align)
  1027  }
  1028  
  1029  func (t *Type) SimpleString() string {
  1030  	return t.kind.String()
  1031  }
  1032  
  1033  // Cmp is a comparison between values a and b.
  1034  //
  1035  //	-1 if a < b
  1036  //	 0 if a == b
  1037  //	 1 if a > b
  1038  type Cmp int8
  1039  
  1040  const (
  1041  	CMPlt = Cmp(-1)
  1042  	CMPeq = Cmp(0)
  1043  	CMPgt = Cmp(1)
  1044  )
  1045  
  1046  // Compare compares types for purposes of the SSA back
  1047  // end, returning a Cmp (one of CMPlt, CMPeq, CMPgt).
  1048  // The answers are correct for an optimizer
  1049  // or code generator, but not necessarily typechecking.
  1050  // The order chosen is arbitrary, only consistency and division
  1051  // into equivalence classes (Types that compare CMPeq) matters.
  1052  func (t *Type) Compare(x *Type) Cmp {
  1053  	if x == t {
  1054  		return CMPeq
  1055  	}
  1056  	return t.cmp(x)
  1057  }
  1058  
  1059  func cmpForNe(x bool) Cmp {
  1060  	if x {
  1061  		return CMPlt
  1062  	}
  1063  	return CMPgt
  1064  }
  1065  
  1066  func (r *Sym) cmpsym(s *Sym) Cmp {
  1067  	if r == s {
  1068  		return CMPeq
  1069  	}
  1070  	if r == nil {
  1071  		return CMPlt
  1072  	}
  1073  	if s == nil {
  1074  		return CMPgt
  1075  	}
  1076  	// Fast sort, not pretty sort
  1077  	if len(r.Name) != len(s.Name) {
  1078  		return cmpForNe(len(r.Name) < len(s.Name))
  1079  	}
  1080  	if r.Pkg != s.Pkg {
  1081  		if len(r.Pkg.Prefix) != len(s.Pkg.Prefix) {
  1082  			return cmpForNe(len(r.Pkg.Prefix) < len(s.Pkg.Prefix))
  1083  		}
  1084  		if r.Pkg.Prefix != s.Pkg.Prefix {
  1085  			return cmpForNe(r.Pkg.Prefix < s.Pkg.Prefix)
  1086  		}
  1087  	}
  1088  	if r.Name != s.Name {
  1089  		return cmpForNe(r.Name < s.Name)
  1090  	}
  1091  	return CMPeq
  1092  }
  1093  
  1094  // cmp compares two *Types t and x, returning CMPlt,
  1095  // CMPeq, CMPgt as t<x, t==x, t>x, for an arbitrary
  1096  // and optimizer-centric notion of comparison.
  1097  // TODO(josharian): make this safe for recursive interface types
  1098  // and use in signatlist sorting. See issue 19869.
  1099  func (t *Type) cmp(x *Type) Cmp {
  1100  	// This follows the structure of function identical in identity.go
  1101  	// with two exceptions.
  1102  	// 1. Symbols are compared more carefully because a <,=,> result is desired.
  1103  	// 2. Maps are treated specially to avoid endless recursion -- maps
  1104  	//    contain an internal data type not expressible in Go source code.
  1105  	if t == x {
  1106  		return CMPeq
  1107  	}
  1108  	if t == nil {
  1109  		return CMPlt
  1110  	}
  1111  	if x == nil {
  1112  		return CMPgt
  1113  	}
  1114  
  1115  	if t.kind != x.kind {
  1116  		return cmpForNe(t.kind < x.kind)
  1117  	}
  1118  
  1119  	if t.obj != nil || x.obj != nil {
  1120  		// Special case: we keep byte and uint8 separate
  1121  		// for error messages. Treat them as equal.
  1122  		switch t.kind {
  1123  		case TUINT8:
  1124  			if (t == Types[TUINT8] || t == ByteType) && (x == Types[TUINT8] || x == ByteType) {
  1125  				return CMPeq
  1126  			}
  1127  
  1128  		case TINT32:
  1129  			if (t == Types[RuneType.kind] || t == RuneType) && (x == Types[RuneType.kind] || x == RuneType) {
  1130  				return CMPeq
  1131  			}
  1132  
  1133  		case TINTER:
  1134  			// Make sure named any type matches any empty interface.
  1135  			if t == AnyType && x.IsEmptyInterface() || x == AnyType && t.IsEmptyInterface() {
  1136  				return CMPeq
  1137  			}
  1138  		}
  1139  	}
  1140  
  1141  	if c := t.Sym().cmpsym(x.Sym()); c != CMPeq {
  1142  		return c
  1143  	}
  1144  
  1145  	if x.obj != nil {
  1146  		return CMPeq
  1147  	}
  1148  	// both syms nil, look at structure below.
  1149  
  1150  	switch t.kind {
  1151  	case TBOOL, TFLOAT32, TFLOAT64, TCOMPLEX64, TCOMPLEX128, TUNSAFEPTR, TUINTPTR,
  1152  		TINT8, TINT16, TINT32, TINT64, TINT, TUINT8, TUINT16, TUINT32, TUINT64, TUINT:
  1153  		return CMPeq
  1154  
  1155  	case TSSA:
  1156  		tname := t.extra.(string)
  1157  		xname := x.extra.(string)
  1158  		// desire fast sorting, not pretty sorting.
  1159  		if len(tname) == len(xname) {
  1160  			if tname == xname {
  1161  				return CMPeq
  1162  			}
  1163  			if tname < xname {
  1164  				return CMPlt
  1165  			}
  1166  			return CMPgt
  1167  		}
  1168  		if len(tname) > len(xname) {
  1169  			return CMPgt
  1170  		}
  1171  		return CMPlt
  1172  
  1173  	case TTUPLE:
  1174  		xtup := x.extra.(*Tuple)
  1175  		ttup := t.extra.(*Tuple)
  1176  		if c := ttup.first.Compare(xtup.first); c != CMPeq {
  1177  			return c
  1178  		}
  1179  		return ttup.second.Compare(xtup.second)
  1180  
  1181  	case TRESULTS:
  1182  		xResults := x.extra.(*Results)
  1183  		tResults := t.extra.(*Results)
  1184  		xl, tl := len(xResults.Types), len(tResults.Types)
  1185  		if tl != xl {
  1186  			if tl < xl {
  1187  				return CMPlt
  1188  			}
  1189  			return CMPgt
  1190  		}
  1191  		for i := 0; i < tl; i++ {
  1192  			if c := tResults.Types[i].Compare(xResults.Types[i]); c != CMPeq {
  1193  				return c
  1194  			}
  1195  		}
  1196  		return CMPeq
  1197  
  1198  	case TMAP:
  1199  		if c := t.Key().cmp(x.Key()); c != CMPeq {
  1200  			return c
  1201  		}
  1202  		return t.Elem().cmp(x.Elem())
  1203  
  1204  	case TPTR, TSLICE:
  1205  		// No special cases for these, they are handled
  1206  		// by the general code after the switch.
  1207  
  1208  	case TSTRUCT:
  1209  		if t.StructType().Map == nil {
  1210  			if x.StructType().Map != nil {
  1211  				return CMPlt // nil < non-nil
  1212  			}
  1213  			// to the fallthrough
  1214  		} else if x.StructType().Map == nil {
  1215  			return CMPgt // nil > non-nil
  1216  		} else if t.StructType().Map.MapType().Bucket == t {
  1217  			// Both have non-nil Map
  1218  			// Special case for Maps which include a recursive type where the recursion is not broken with a named type
  1219  			if x.StructType().Map.MapType().Bucket != x {
  1220  				return CMPlt // bucket maps are least
  1221  			}
  1222  			return t.StructType().Map.cmp(x.StructType().Map)
  1223  		} else if x.StructType().Map.MapType().Bucket == x {
  1224  			return CMPgt // bucket maps are least
  1225  		} // If t != t.Map.Bucket, fall through to general case
  1226  
  1227  		tfs := t.Fields()
  1228  		xfs := x.Fields()
  1229  		for i := 0; i < len(tfs) && i < len(xfs); i++ {
  1230  			t1, x1 := tfs[i], xfs[i]
  1231  			if t1.Embedded != x1.Embedded {
  1232  				return cmpForNe(t1.Embedded < x1.Embedded)
  1233  			}
  1234  			if t1.Note != x1.Note {
  1235  				return cmpForNe(t1.Note < x1.Note)
  1236  			}
  1237  			if c := t1.Sym.cmpsym(x1.Sym); c != CMPeq {
  1238  				return c
  1239  			}
  1240  			if c := t1.Type.cmp(x1.Type); c != CMPeq {
  1241  				return c
  1242  			}
  1243  		}
  1244  		if len(tfs) != len(xfs) {
  1245  			return cmpForNe(len(tfs) < len(xfs))
  1246  		}
  1247  		return CMPeq
  1248  
  1249  	case TINTER:
  1250  		tfs := t.AllMethods()
  1251  		xfs := x.AllMethods()
  1252  		for i := 0; i < len(tfs) && i < len(xfs); i++ {
  1253  			t1, x1 := tfs[i], xfs[i]
  1254  			if c := t1.Sym.cmpsym(x1.Sym); c != CMPeq {
  1255  				return c
  1256  			}
  1257  			if c := t1.Type.cmp(x1.Type); c != CMPeq {
  1258  				return c
  1259  			}
  1260  		}
  1261  		if len(tfs) != len(xfs) {
  1262  			return cmpForNe(len(tfs) < len(xfs))
  1263  		}
  1264  		return CMPeq
  1265  
  1266  	case TFUNC:
  1267  		if tn, xn := t.NumRecvs(), x.NumRecvs(); tn != xn {
  1268  			return cmpForNe(tn < xn)
  1269  		}
  1270  		if tn, xn := t.NumParams(), x.NumParams(); tn != xn {
  1271  			return cmpForNe(tn < xn)
  1272  		}
  1273  		if tn, xn := t.NumResults(), x.NumResults(); tn != xn {
  1274  			return cmpForNe(tn < xn)
  1275  		}
  1276  		if tv, xv := t.IsVariadic(), x.IsVariadic(); tv != xv {
  1277  			return cmpForNe(!tv)
  1278  		}
  1279  
  1280  		tfs := t.RecvParamsResults()
  1281  		xfs := x.RecvParamsResults()
  1282  		for i, tf := range tfs {
  1283  			if c := tf.Type.cmp(xfs[i].Type); c != CMPeq {
  1284  				return c
  1285  			}
  1286  		}
  1287  		return CMPeq
  1288  
  1289  	case TARRAY:
  1290  		if t.NumElem() != x.NumElem() {
  1291  			return cmpForNe(t.NumElem() < x.NumElem())
  1292  		}
  1293  
  1294  	case TCHAN:
  1295  		if t.ChanDir() != x.ChanDir() {
  1296  			return cmpForNe(t.ChanDir() < x.ChanDir())
  1297  		}
  1298  
  1299  	default:
  1300  		e := fmt.Sprintf("Do not know how to compare %v with %v", t, x)
  1301  		panic(e)
  1302  	}
  1303  
  1304  	// Common element type comparison for TARRAY, TCHAN, TPTR, and TSLICE.
  1305  	return t.Elem().cmp(x.Elem())
  1306  }
  1307  
  1308  // IsKind reports whether t is a Type of the specified kind.
  1309  func (t *Type) IsKind(et Kind) bool {
  1310  	return t != nil && t.kind == et
  1311  }
  1312  
  1313  func (t *Type) IsBoolean() bool {
  1314  	return t.kind == TBOOL
  1315  }
  1316  
  1317  var unsignedEType = [...]Kind{
  1318  	TINT8:    TUINT8,
  1319  	TUINT8:   TUINT8,
  1320  	TINT16:   TUINT16,
  1321  	TUINT16:  TUINT16,
  1322  	TINT32:   TUINT32,
  1323  	TUINT32:  TUINT32,
  1324  	TINT64:   TUINT64,
  1325  	TUINT64:  TUINT64,
  1326  	TINT:     TUINT,
  1327  	TUINT:    TUINT,
  1328  	TUINTPTR: TUINTPTR,
  1329  }
  1330  
  1331  // ToUnsigned returns the unsigned equivalent of integer type t.
  1332  func (t *Type) ToUnsigned() *Type {
  1333  	if !t.IsInteger() {
  1334  		base.Fatalf("unsignedType(%v)", t)
  1335  	}
  1336  	return Types[unsignedEType[t.kind]]
  1337  }
  1338  
  1339  func (t *Type) IsInteger() bool {
  1340  	switch t.kind {
  1341  	case TINT8, TUINT8, TINT16, TUINT16, TINT32, TUINT32, TINT64, TUINT64, TINT, TUINT, TUINTPTR:
  1342  		return true
  1343  	}
  1344  	return t == UntypedInt || t == UntypedRune
  1345  }
  1346  
  1347  func (t *Type) IsSigned() bool {
  1348  	switch t.kind {
  1349  	case TINT8, TINT16, TINT32, TINT64, TINT:
  1350  		return true
  1351  	}
  1352  	return false
  1353  }
  1354  
  1355  func (t *Type) IsUnsigned() bool {
  1356  	switch t.kind {
  1357  	case TUINT8, TUINT16, TUINT32, TUINT64, TUINT, TUINTPTR:
  1358  		return true
  1359  	}
  1360  	return false
  1361  }
  1362  
  1363  func (t *Type) IsFloat() bool {
  1364  	return t.kind == TFLOAT32 || t.kind == TFLOAT64 || t == UntypedFloat
  1365  }
  1366  
  1367  func (t *Type) IsComplex() bool {
  1368  	return t.kind == TCOMPLEX64 || t.kind == TCOMPLEX128 || t == UntypedComplex
  1369  }
  1370  
  1371  // IsPtr reports whether t is a regular Go pointer type.
  1372  // This does not include unsafe.Pointer.
  1373  func (t *Type) IsPtr() bool {
  1374  	return t.kind == TPTR
  1375  }
  1376  
  1377  // IsPtrElem reports whether t is the element of a pointer (to t).
  1378  func (t *Type) IsPtrElem() bool {
  1379  	return t.cache.ptr != nil
  1380  }
  1381  
  1382  // IsUnsafePtr reports whether t is an unsafe pointer.
  1383  func (t *Type) IsUnsafePtr() bool {
  1384  	return t.kind == TUNSAFEPTR
  1385  }
  1386  
  1387  // IsUintptr reports whether t is a uintptr.
  1388  func (t *Type) IsUintptr() bool {
  1389  	return t.kind == TUINTPTR
  1390  }
  1391  
  1392  // IsPtrShaped reports whether t is represented by a single machine pointer.
  1393  // In addition to regular Go pointer types, this includes map, channel, and
  1394  // function types and unsafe.Pointer. It does not include array or struct types
  1395  // that consist of a single pointer shaped type.
  1396  // TODO(mdempsky): Should it? See golang.org/issue/15028.
  1397  func (t *Type) IsPtrShaped() bool {
  1398  	return t.kind == TPTR || t.kind == TUNSAFEPTR ||
  1399  		t.kind == TMAP || t.kind == TCHAN || t.kind == TFUNC
  1400  }
  1401  
  1402  // HasNil reports whether the set of values determined by t includes nil.
  1403  func (t *Type) HasNil() bool {
  1404  	switch t.kind {
  1405  	case TCHAN, TFUNC, TINTER, TMAP, TNIL, TPTR, TSLICE, TUNSAFEPTR:
  1406  		return true
  1407  	}
  1408  	return false
  1409  }
  1410  
  1411  func (t *Type) IsString() bool {
  1412  	return t.kind == TSTRING
  1413  }
  1414  
  1415  func (t *Type) IsMap() bool {
  1416  	return t.kind == TMAP
  1417  }
  1418  
  1419  func (t *Type) IsChan() bool {
  1420  	return t.kind == TCHAN
  1421  }
  1422  
  1423  func (t *Type) IsSlice() bool {
  1424  	return t.kind == TSLICE
  1425  }
  1426  
  1427  func (t *Type) IsArray() bool {
  1428  	return t.kind == TARRAY
  1429  }
  1430  
  1431  func (t *Type) IsStruct() bool {
  1432  	return t.kind == TSTRUCT
  1433  }
  1434  
  1435  func (t *Type) IsInterface() bool {
  1436  	return t.kind == TINTER
  1437  }
  1438  
  1439  // IsEmptyInterface reports whether t is an empty interface type.
  1440  func (t *Type) IsEmptyInterface() bool {
  1441  	return t.IsInterface() && len(t.AllMethods()) == 0
  1442  }
  1443  
  1444  // IsScalar reports whether 't' is a scalar Go type, e.g.
  1445  // bool/int/float/complex. Note that struct and array types consisting
  1446  // of a single scalar element are not considered scalar, likewise
  1447  // pointer types are also not considered scalar.
  1448  func (t *Type) IsScalar() bool {
  1449  	switch t.kind {
  1450  	case TBOOL, TINT8, TUINT8, TINT16, TUINT16, TINT32,
  1451  		TUINT32, TINT64, TUINT64, TINT, TUINT,
  1452  		TUINTPTR, TCOMPLEX64, TCOMPLEX128, TFLOAT32, TFLOAT64:
  1453  		return true
  1454  	}
  1455  	return false
  1456  }
  1457  
  1458  func (t *Type) PtrTo() *Type {
  1459  	return NewPtr(t)
  1460  }
  1461  
  1462  func (t *Type) NumFields() int {
  1463  	if t.kind == TRESULTS {
  1464  		return len(t.extra.(*Results).Types)
  1465  	}
  1466  	return len(t.Fields())
  1467  }
  1468  func (t *Type) FieldType(i int) *Type {
  1469  	if t.kind == TTUPLE {
  1470  		switch i {
  1471  		case 0:
  1472  			return t.extra.(*Tuple).first
  1473  		case 1:
  1474  			return t.extra.(*Tuple).second
  1475  		default:
  1476  			panic("bad tuple index")
  1477  		}
  1478  	}
  1479  	if t.kind == TRESULTS {
  1480  		return t.extra.(*Results).Types[i]
  1481  	}
  1482  	return t.Field(i).Type
  1483  }
  1484  func (t *Type) FieldOff(i int) int64 {
  1485  	return t.Field(i).Offset
  1486  }
  1487  func (t *Type) FieldName(i int) string {
  1488  	return t.Field(i).Sym.Name
  1489  }
  1490  
  1491  // OffsetOf reports the offset of the field of a struct.
  1492  // The field is looked up by name.
  1493  func (t *Type) OffsetOf(name string) int64 {
  1494  	if t.kind != TSTRUCT {
  1495  		base.Fatalf("can't call OffsetOf on non-struct %v", t)
  1496  	}
  1497  	for _, f := range t.Fields() {
  1498  		if f.Sym.Name == name {
  1499  			return f.Offset
  1500  		}
  1501  	}
  1502  	base.Fatalf("couldn't find field %s in %v", name, t)
  1503  	return -1
  1504  }
  1505  
  1506  func (t *Type) NumElem() int64 {
  1507  	t.wantEtype(TARRAY)
  1508  	return t.extra.(*Array).Bound
  1509  }
  1510  
  1511  type componentsIncludeBlankFields bool
  1512  
  1513  const (
  1514  	IgnoreBlankFields componentsIncludeBlankFields = false
  1515  	CountBlankFields  componentsIncludeBlankFields = true
  1516  )
  1517  
  1518  // NumComponents returns the number of primitive elements that compose t.
  1519  // Struct and array types are flattened for the purpose of counting.
  1520  // All other types (including string, slice, and interface types) count as one element.
  1521  // If countBlank is IgnoreBlankFields, then blank struct fields
  1522  // (and their comprised elements) are excluded from the count.
  1523  // struct { x, y [3]int } has six components; [10]struct{ x, y string } has twenty.
  1524  func (t *Type) NumComponents(countBlank componentsIncludeBlankFields) int64 {
  1525  	switch t.kind {
  1526  	case TSTRUCT:
  1527  		if t.IsFuncArgStruct() {
  1528  			base.Fatalf("NumComponents func arg struct")
  1529  		}
  1530  		var n int64
  1531  		for _, f := range t.Fields() {
  1532  			if countBlank == IgnoreBlankFields && f.Sym.IsBlank() {
  1533  				continue
  1534  			}
  1535  			n += f.Type.NumComponents(countBlank)
  1536  		}
  1537  		return n
  1538  	case TARRAY:
  1539  		return t.NumElem() * t.Elem().NumComponents(countBlank)
  1540  	}
  1541  	return 1
  1542  }
  1543  
  1544  // SoleComponent returns the only primitive component in t,
  1545  // if there is exactly one. Otherwise, it returns nil.
  1546  // Components are counted as in NumComponents, including blank fields.
  1547  // Keep in sync with cmd/compile/internal/walk/convert.go:soleComponent.
  1548  func (t *Type) SoleComponent() *Type {
  1549  	switch t.kind {
  1550  	case TSTRUCT:
  1551  		if t.IsFuncArgStruct() {
  1552  			base.Fatalf("SoleComponent func arg struct")
  1553  		}
  1554  		if t.NumFields() != 1 {
  1555  			return nil
  1556  		}
  1557  		return t.Field(0).Type.SoleComponent()
  1558  	case TARRAY:
  1559  		if t.NumElem() != 1 {
  1560  			return nil
  1561  		}
  1562  		return t.Elem().SoleComponent()
  1563  	}
  1564  	return t
  1565  }
  1566  
  1567  // ChanDir returns the direction of a channel type t.
  1568  // The direction will be one of Crecv, Csend, or Cboth.
  1569  func (t *Type) ChanDir() ChanDir {
  1570  	t.wantEtype(TCHAN)
  1571  	return t.extra.(*Chan).Dir
  1572  }
  1573  
  1574  func (t *Type) IsMemory() bool {
  1575  	if t == TypeMem || t.kind == TTUPLE && t.extra.(*Tuple).second == TypeMem {
  1576  		return true
  1577  	}
  1578  	if t.kind == TRESULTS {
  1579  		if types := t.extra.(*Results).Types; len(types) > 0 && types[len(types)-1] == TypeMem {
  1580  			return true
  1581  		}
  1582  	}
  1583  	return false
  1584  }
  1585  func (t *Type) IsFlags() bool   { return t == TypeFlags }
  1586  func (t *Type) IsVoid() bool    { return t == TypeVoid }
  1587  func (t *Type) IsTuple() bool   { return t.kind == TTUPLE }
  1588  func (t *Type) IsResults() bool { return t.kind == TRESULTS }
  1589  
  1590  // IsUntyped reports whether t is an untyped type.
  1591  func (t *Type) IsUntyped() bool {
  1592  	if t == nil {
  1593  		return false
  1594  	}
  1595  	if t == UntypedString || t == UntypedBool {
  1596  		return true
  1597  	}
  1598  	switch t.kind {
  1599  	case TNIL, TIDEAL:
  1600  		return true
  1601  	}
  1602  	return false
  1603  }
  1604  
  1605  // HasPointers reports whether t contains a heap pointer.
  1606  // Note that this function ignores pointers to not-in-heap types.
  1607  func (t *Type) HasPointers() bool {
  1608  	return PtrDataSize(t) > 0
  1609  }
  1610  
  1611  var recvType *Type
  1612  
  1613  // FakeRecvType returns the singleton type used for interface method receivers.
  1614  func FakeRecvType() *Type {
  1615  	if recvType == nil {
  1616  		recvType = NewPtr(newType(TSTRUCT))
  1617  	}
  1618  	return recvType
  1619  }
  1620  
  1621  func FakeRecv() *Field {
  1622  	return NewField(base.AutogeneratedPos, nil, FakeRecvType())
  1623  }
  1624  
  1625  var (
  1626  	// TSSA types. HasPointers assumes these are pointer-free.
  1627  	TypeInvalid   = newSSA("invalid")
  1628  	TypeMem       = newSSA("mem")
  1629  	TypeFlags     = newSSA("flags")
  1630  	TypeVoid      = newSSA("void")
  1631  	TypeInt128    = newSSA("int128")
  1632  	TypeResultMem = newResults([]*Type{TypeMem})
  1633  )
  1634  
  1635  func init() {
  1636  	TypeInt128.width = 16
  1637  	TypeInt128.align = 8
  1638  }
  1639  
  1640  // NewNamed returns a new named type for the given type name. obj should be an
  1641  // ir.Name. The new type is incomplete (marked as TFORW kind), and the underlying
  1642  // type should be set later via SetUnderlying(). References to the type are
  1643  // maintained until the type is filled in, so those references can be updated when
  1644  // the type is complete.
  1645  func NewNamed(obj Object) *Type {
  1646  	t := newType(TFORW)
  1647  	t.obj = obj
  1648  	sym := obj.Sym()
  1649  	if sym.Pkg == ShapePkg {
  1650  		t.SetIsShape(true)
  1651  		t.SetHasShape(true)
  1652  	}
  1653  	if sym.Pkg.Path == "runtime/internal/sys" && sym.Name == "nih" {
  1654  		// Recognize the special not-in-heap type. Any type including
  1655  		// this type will also be not-in-heap.
  1656  		// This logic is duplicated in go/types and
  1657  		// cmd/compile/internal/types2.
  1658  		t.SetNotInHeap(true)
  1659  	}
  1660  	return t
  1661  }
  1662  
  1663  // Obj returns the canonical type name node for a named type t, nil for an unnamed type.
  1664  func (t *Type) Obj() Object {
  1665  	return t.obj
  1666  }
  1667  
  1668  // SetUnderlying sets the underlying type of an incomplete type (i.e. type whose kind
  1669  // is currently TFORW). SetUnderlying automatically updates any types that were waiting
  1670  // for this type to be completed.
  1671  func (t *Type) SetUnderlying(underlying *Type) {
  1672  	if underlying.kind == TFORW {
  1673  		// This type isn't computed yet; when it is, update n.
  1674  		underlying.forwardType().Copyto = append(underlying.forwardType().Copyto, t)
  1675  		return
  1676  	}
  1677  
  1678  	ft := t.forwardType()
  1679  
  1680  	// TODO(mdempsky): Fix Type rekinding.
  1681  	t.kind = underlying.kind
  1682  	t.extra = underlying.extra
  1683  	t.width = underlying.width
  1684  	t.align = underlying.align
  1685  	t.alg = underlying.alg
  1686  	t.ptrBytes = underlying.ptrBytes
  1687  	t.intRegs = underlying.intRegs
  1688  	t.floatRegs = underlying.floatRegs
  1689  	t.underlying = underlying.underlying
  1690  
  1691  	if underlying.NotInHeap() {
  1692  		t.SetNotInHeap(true)
  1693  	}
  1694  	if underlying.HasShape() {
  1695  		t.SetHasShape(true)
  1696  	}
  1697  
  1698  	// spec: "The declared type does not inherit any methods bound
  1699  	// to the existing type, but the method set of an interface
  1700  	// type [...] remains unchanged."
  1701  	if t.IsInterface() {
  1702  		t.methods = underlying.methods
  1703  		t.allMethods = underlying.allMethods
  1704  	}
  1705  
  1706  	// Update types waiting on this type.
  1707  	for _, w := range ft.Copyto {
  1708  		w.SetUnderlying(t)
  1709  	}
  1710  
  1711  	// Double-check use of type as embedded type.
  1712  	if ft.Embedlineno.IsKnown() {
  1713  		if t.IsPtr() || t.IsUnsafePtr() {
  1714  			base.ErrorfAt(ft.Embedlineno, errors.InvalidPtrEmbed, "embedded type cannot be a pointer")
  1715  		}
  1716  	}
  1717  }
  1718  
  1719  func fieldsHasShape(fields []*Field) bool {
  1720  	for _, f := range fields {
  1721  		if f.Type != nil && f.Type.HasShape() {
  1722  			return true
  1723  		}
  1724  	}
  1725  	return false
  1726  }
  1727  
  1728  // newBasic returns a new basic type of the given kind.
  1729  func newBasic(kind Kind, obj Object) *Type {
  1730  	t := newType(kind)
  1731  	t.obj = obj
  1732  	return t
  1733  }
  1734  
  1735  // NewInterface returns a new interface for the given methods and
  1736  // embedded types. Embedded types are specified as fields with no Sym.
  1737  func NewInterface(methods []*Field) *Type {
  1738  	t := newType(TINTER)
  1739  	t.SetInterface(methods)
  1740  	for _, f := range methods {
  1741  		// f.Type could be nil for a broken interface declaration
  1742  		if f.Type != nil && f.Type.HasShape() {
  1743  			t.SetHasShape(true)
  1744  			break
  1745  		}
  1746  	}
  1747  	return t
  1748  }
  1749  
  1750  // NewSignature returns a new function type for the given receiver,
  1751  // parameters, and results, any of which may be nil.
  1752  func NewSignature(recv *Field, params, results []*Field) *Type {
  1753  	startParams := 0
  1754  	if recv != nil {
  1755  		startParams = 1
  1756  	}
  1757  	startResults := startParams + len(params)
  1758  
  1759  	allParams := make([]*Field, startResults+len(results))
  1760  	if recv != nil {
  1761  		allParams[0] = recv
  1762  	}
  1763  	copy(allParams[startParams:], params)
  1764  	copy(allParams[startResults:], results)
  1765  
  1766  	t := newType(TFUNC)
  1767  	ft := t.funcType()
  1768  
  1769  	funargs := func(fields []*Field) *Type {
  1770  		s := NewStruct(fields)
  1771  		s.StructType().ParamTuple = true
  1772  		return s
  1773  	}
  1774  
  1775  	ft.allParams = allParams
  1776  	ft.startParams = startParams
  1777  	ft.startResults = startResults
  1778  
  1779  	ft.resultsTuple = funargs(allParams[startResults:])
  1780  
  1781  	if fieldsHasShape(allParams) {
  1782  		t.SetHasShape(true)
  1783  	}
  1784  
  1785  	return t
  1786  }
  1787  
  1788  // NewStruct returns a new struct with the given fields.
  1789  func NewStruct(fields []*Field) *Type {
  1790  	t := newType(TSTRUCT)
  1791  	t.setFields(fields)
  1792  	if fieldsHasShape(fields) {
  1793  		t.SetHasShape(true)
  1794  	}
  1795  	for _, f := range fields {
  1796  		if f.Type.NotInHeap() {
  1797  			t.SetNotInHeap(true)
  1798  			break
  1799  		}
  1800  	}
  1801  
  1802  	return t
  1803  }
  1804  
  1805  var (
  1806  	IsInt     [NTYPE]bool
  1807  	IsFloat   [NTYPE]bool
  1808  	IsComplex [NTYPE]bool
  1809  	IsSimple  [NTYPE]bool
  1810  )
  1811  
  1812  var IsOrdered [NTYPE]bool
  1813  
  1814  // IsReflexive reports whether t has a reflexive equality operator.
  1815  // That is, if x==x for all x of type t.
  1816  func IsReflexive(t *Type) bool {
  1817  	switch t.Kind() {
  1818  	case TBOOL,
  1819  		TINT,
  1820  		TUINT,
  1821  		TINT8,
  1822  		TUINT8,
  1823  		TINT16,
  1824  		TUINT16,
  1825  		TINT32,
  1826  		TUINT32,
  1827  		TINT64,
  1828  		TUINT64,
  1829  		TUINTPTR,
  1830  		TPTR,
  1831  		TUNSAFEPTR,
  1832  		TSTRING,
  1833  		TCHAN:
  1834  		return true
  1835  
  1836  	case TFLOAT32,
  1837  		TFLOAT64,
  1838  		TCOMPLEX64,
  1839  		TCOMPLEX128,
  1840  		TINTER:
  1841  		return false
  1842  
  1843  	case TARRAY:
  1844  		return IsReflexive(t.Elem())
  1845  
  1846  	case TSTRUCT:
  1847  		for _, t1 := range t.Fields() {
  1848  			if !IsReflexive(t1.Type) {
  1849  				return false
  1850  			}
  1851  		}
  1852  		return true
  1853  
  1854  	default:
  1855  		base.Fatalf("bad type for map key: %v", t)
  1856  		return false
  1857  	}
  1858  }
  1859  
  1860  // Can this type be stored directly in an interface word?
  1861  // Yes, if the representation is a single pointer.
  1862  func IsDirectIface(t *Type) bool {
  1863  	switch t.Kind() {
  1864  	case TPTR:
  1865  		// Pointers to notinheap types must be stored indirectly. See issue 42076.
  1866  		return !t.Elem().NotInHeap()
  1867  	case TCHAN,
  1868  		TMAP,
  1869  		TFUNC,
  1870  		TUNSAFEPTR:
  1871  		return true
  1872  
  1873  	case TARRAY:
  1874  		// Array of 1 direct iface type can be direct.
  1875  		return t.NumElem() == 1 && IsDirectIface(t.Elem())
  1876  
  1877  	case TSTRUCT:
  1878  		// Struct with 1 field of direct iface type can be direct.
  1879  		return t.NumFields() == 1 && IsDirectIface(t.Field(0).Type)
  1880  	}
  1881  
  1882  	return false
  1883  }
  1884  
  1885  // IsInterfaceMethod reports whether (field) m is
  1886  // an interface method. Such methods have the
  1887  // special receiver type types.FakeRecvType().
  1888  func IsInterfaceMethod(f *Type) bool {
  1889  	return f.Recv().Type == FakeRecvType()
  1890  }
  1891  
  1892  // IsMethodApplicable reports whether method m can be called on a
  1893  // value of type t. This is necessary because we compute a single
  1894  // method set for both T and *T, but some *T methods are not
  1895  // applicable to T receivers.
  1896  func IsMethodApplicable(t *Type, m *Field) bool {
  1897  	return t.IsPtr() || !m.Type.Recv().Type.IsPtr() || IsInterfaceMethod(m.Type) || m.Embedded == 2
  1898  }
  1899  
  1900  // RuntimeSymName returns the name of s if it's in package "runtime"; otherwise
  1901  // it returns "".
  1902  func RuntimeSymName(s *Sym) string {
  1903  	if s.Pkg.Path == "runtime" {
  1904  		return s.Name
  1905  	}
  1906  	return ""
  1907  }
  1908  
  1909  // ReflectSymName returns the name of s if it's in package "reflect"; otherwise
  1910  // it returns "".
  1911  func ReflectSymName(s *Sym) string {
  1912  	if s.Pkg.Path == "reflect" {
  1913  		return s.Name
  1914  	}
  1915  	return ""
  1916  }
  1917  
  1918  // IsNoInstrumentPkg reports whether p is a package that
  1919  // should not be instrumented.
  1920  func IsNoInstrumentPkg(p *Pkg) bool {
  1921  	return objabi.LookupPkgSpecial(p.Path).NoInstrument
  1922  }
  1923  
  1924  // IsNoRacePkg reports whether p is a package that
  1925  // should not be race instrumented.
  1926  func IsNoRacePkg(p *Pkg) bool {
  1927  	return objabi.LookupPkgSpecial(p.Path).NoRaceFunc
  1928  }
  1929  
  1930  // IsRuntimePkg reports whether p is a runtime package.
  1931  func IsRuntimePkg(p *Pkg) bool {
  1932  	return objabi.LookupPkgSpecial(p.Path).Runtime
  1933  }
  1934  
  1935  // ReceiverBaseType returns the underlying type, if any,
  1936  // that owns methods with receiver parameter t.
  1937  // The result is either a named type or an anonymous struct.
  1938  func ReceiverBaseType(t *Type) *Type {
  1939  	if t == nil {
  1940  		return nil
  1941  	}
  1942  
  1943  	// Strip away pointer if it's there.
  1944  	if t.IsPtr() {
  1945  		if t.Sym() != nil {
  1946  			return nil
  1947  		}
  1948  		t = t.Elem()
  1949  		if t == nil {
  1950  			return nil
  1951  		}
  1952  	}
  1953  
  1954  	// Must be a named type or anonymous struct.
  1955  	if t.Sym() == nil && !t.IsStruct() {
  1956  		return nil
  1957  	}
  1958  
  1959  	// Check types.
  1960  	if IsSimple[t.Kind()] {
  1961  		return t
  1962  	}
  1963  	switch t.Kind() {
  1964  	case TARRAY, TCHAN, TFUNC, TMAP, TSLICE, TSTRING, TSTRUCT:
  1965  		return t
  1966  	}
  1967  	return nil
  1968  }
  1969  
  1970  func FloatForComplex(t *Type) *Type {
  1971  	switch t.Kind() {
  1972  	case TCOMPLEX64:
  1973  		return Types[TFLOAT32]
  1974  	case TCOMPLEX128:
  1975  		return Types[TFLOAT64]
  1976  	}
  1977  	base.Fatalf("unexpected type: %v", t)
  1978  	return nil
  1979  }
  1980  
  1981  func ComplexForFloat(t *Type) *Type {
  1982  	switch t.Kind() {
  1983  	case TFLOAT32:
  1984  		return Types[TCOMPLEX64]
  1985  	case TFLOAT64:
  1986  		return Types[TCOMPLEX128]
  1987  	}
  1988  	base.Fatalf("unexpected type: %v", t)
  1989  	return nil
  1990  }
  1991  
  1992  func TypeSym(t *Type) *Sym {
  1993  	return TypeSymLookup(TypeSymName(t))
  1994  }
  1995  
  1996  func TypeSymLookup(name string) *Sym {
  1997  	typepkgmu.Lock()
  1998  	s := typepkg.Lookup(name)
  1999  	typepkgmu.Unlock()
  2000  	return s
  2001  }
  2002  
  2003  func TypeSymName(t *Type) string {
  2004  	name := t.LinkString()
  2005  	// Use a separate symbol name for Noalg types for #17752.
  2006  	if TypeHasNoAlg(t) {
  2007  		name = "noalg." + name
  2008  	}
  2009  	return name
  2010  }
  2011  
  2012  // Fake package for runtime type info (headers)
  2013  // Don't access directly, use typeLookup below.
  2014  var (
  2015  	typepkgmu sync.Mutex // protects typepkg lookups
  2016  	typepkg   = NewPkg("type", "type")
  2017  )
  2018  
  2019  var SimType [NTYPE]Kind
  2020  
  2021  // Fake package for shape types (see typecheck.Shapify()).
  2022  var ShapePkg = NewPkg("go.shape", "go.shape")
  2023  

View as plain text