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

View as plain text