Black Lives Matter. Support the Equal Justice Initiative.

Source file src/runtime/type.go

Documentation: runtime

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Runtime type representation.
     6  
     7  package runtime
     8  
     9  import "unsafe"
    10  
    11  // tflag is documented in reflect/type.go.
    12  //
    13  // tflag values must be kept in sync with copies in:
    14  //	cmd/compile/internal/gc/reflect.go
    15  //	cmd/link/internal/ld/decodesym.go
    16  //	reflect/type.go
    17  //      internal/reflectlite/type.go
    18  type tflag uint8
    19  
    20  const (
    21  	tflagUncommon      tflag = 1 << 0
    22  	tflagExtraStar     tflag = 1 << 1
    23  	tflagNamed         tflag = 1 << 2
    24  	tflagRegularMemory tflag = 1 << 3 // equal and hash can treat values of this type as a single region of t.size bytes
    25  )
    26  
    27  // Needs to be in sync with ../cmd/link/internal/ld/decodesym.go:/^func.commonsize,
    28  // ../cmd/compile/internal/gc/reflect.go:/^func.dcommontype and
    29  // ../reflect/type.go:/^type.rtype.
    30  // ../internal/reflectlite/type.go:/^type.rtype.
    31  type _type struct {
    32  	size       uintptr
    33  	ptrdata    uintptr // size of memory prefix holding all pointers
    34  	hash       uint32
    35  	tflag      tflag
    36  	align      uint8
    37  	fieldAlign uint8
    38  	kind       uint8
    39  	// function for comparing objects of this type
    40  	// (ptr to object A, ptr to object B) -> ==?
    41  	equal func(unsafe.Pointer, unsafe.Pointer) bool
    42  	// gcdata stores the GC type data for the garbage collector.
    43  	// If the KindGCProg bit is set in kind, gcdata is a GC program.
    44  	// Otherwise it is a ptrmask bitmap. See mbitmap.go for details.
    45  	gcdata    *byte
    46  	str       nameOff
    47  	ptrToThis typeOff
    48  }
    49  
    50  func (t *_type) string() string {
    51  	s := t.nameOff(t.str).name()
    52  	if t.tflag&tflagExtraStar != 0 {
    53  		return s[1:]
    54  	}
    55  	return s
    56  }
    57  
    58  func (t *_type) uncommon() *uncommontype {
    59  	if t.tflag&tflagUncommon == 0 {
    60  		return nil
    61  	}
    62  	switch t.kind & kindMask {
    63  	case kindStruct:
    64  		type u struct {
    65  			structtype
    66  			u uncommontype
    67  		}
    68  		return &(*u)(unsafe.Pointer(t)).u
    69  	case kindPtr:
    70  		type u struct {
    71  			ptrtype
    72  			u uncommontype
    73  		}
    74  		return &(*u)(unsafe.Pointer(t)).u
    75  	case kindFunc:
    76  		type u struct {
    77  			functype
    78  			u uncommontype
    79  		}
    80  		return &(*u)(unsafe.Pointer(t)).u
    81  	case kindSlice:
    82  		type u struct {
    83  			slicetype
    84  			u uncommontype
    85  		}
    86  		return &(*u)(unsafe.Pointer(t)).u
    87  	case kindArray:
    88  		type u struct {
    89  			arraytype
    90  			u uncommontype
    91  		}
    92  		return &(*u)(unsafe.Pointer(t)).u
    93  	case kindChan:
    94  		type u struct {
    95  			chantype
    96  			u uncommontype
    97  		}
    98  		return &(*u)(unsafe.Pointer(t)).u
    99  	case kindMap:
   100  		type u struct {
   101  			maptype
   102  			u uncommontype
   103  		}
   104  		return &(*u)(unsafe.Pointer(t)).u
   105  	case kindInterface:
   106  		type u struct {
   107  			interfacetype
   108  			u uncommontype
   109  		}
   110  		return &(*u)(unsafe.Pointer(t)).u
   111  	default:
   112  		type u struct {
   113  			_type
   114  			u uncommontype
   115  		}
   116  		return &(*u)(unsafe.Pointer(t)).u
   117  	}
   118  }
   119  
   120  func (t *_type) name() string {
   121  	if t.tflag&tflagNamed == 0 {
   122  		return ""
   123  	}
   124  	s := t.string()
   125  	i := len(s) - 1
   126  	for i >= 0 && s[i] != '.' {
   127  		i--
   128  	}
   129  	return s[i+1:]
   130  }
   131  
   132  // pkgpath returns the path of the package where t was defined, if
   133  // available. This is not the same as the reflect package's PkgPath
   134  // method, in that it returns the package path for struct and interface
   135  // types, not just named types.
   136  func (t *_type) pkgpath() string {
   137  	if u := t.uncommon(); u != nil {
   138  		return t.nameOff(u.pkgpath).name()
   139  	}
   140  	switch t.kind & kindMask {
   141  	case kindStruct:
   142  		st := (*structtype)(unsafe.Pointer(t))
   143  		return st.pkgPath.name()
   144  	case kindInterface:
   145  		it := (*interfacetype)(unsafe.Pointer(t))
   146  		return it.pkgpath.name()
   147  	}
   148  	return ""
   149  }
   150  
   151  // reflectOffs holds type offsets defined at run time by the reflect package.
   152  //
   153  // When a type is defined at run time, its *rtype data lives on the heap.
   154  // There are a wide range of possible addresses the heap may use, that
   155  // may not be representable as a 32-bit offset. Moreover the GC may
   156  // one day start moving heap memory, in which case there is no stable
   157  // offset that can be defined.
   158  //
   159  // To provide stable offsets, we add pin *rtype objects in a global map
   160  // and treat the offset as an identifier. We use negative offsets that
   161  // do not overlap with any compile-time module offsets.
   162  //
   163  // Entries are created by reflect.addReflectOff.
   164  var reflectOffs struct {
   165  	lock mutex
   166  	next int32
   167  	m    map[int32]unsafe.Pointer
   168  	minv map[unsafe.Pointer]int32
   169  }
   170  
   171  func reflectOffsLock() {
   172  	lock(&reflectOffs.lock)
   173  	if raceenabled {
   174  		raceacquire(unsafe.Pointer(&reflectOffs.lock))
   175  	}
   176  }
   177  
   178  func reflectOffsUnlock() {
   179  	if raceenabled {
   180  		racerelease(unsafe.Pointer(&reflectOffs.lock))
   181  	}
   182  	unlock(&reflectOffs.lock)
   183  }
   184  
   185  func resolveNameOff(ptrInModule unsafe.Pointer, off nameOff) name {
   186  	if off == 0 {
   187  		return name{}
   188  	}
   189  	base := uintptr(ptrInModule)
   190  	for md := &firstmoduledata; md != nil; md = md.next {
   191  		if base >= md.types && base < md.etypes {
   192  			res := md.types + uintptr(off)
   193  			if res > md.etypes {
   194  				println("runtime: nameOff", hex(off), "out of range", hex(md.types), "-", hex(md.etypes))
   195  				throw("runtime: name offset out of range")
   196  			}
   197  			return name{(*byte)(unsafe.Pointer(res))}
   198  		}
   199  	}
   200  
   201  	// No module found. see if it is a run time name.
   202  	reflectOffsLock()
   203  	res, found := reflectOffs.m[int32(off)]
   204  	reflectOffsUnlock()
   205  	if !found {
   206  		println("runtime: nameOff", hex(off), "base", hex(base), "not in ranges:")
   207  		for next := &firstmoduledata; next != nil; next = next.next {
   208  			println("\ttypes", hex(next.types), "etypes", hex(next.etypes))
   209  		}
   210  		throw("runtime: name offset base pointer out of range")
   211  	}
   212  	return name{(*byte)(res)}
   213  }
   214  
   215  func (t *_type) nameOff(off nameOff) name {
   216  	return resolveNameOff(unsafe.Pointer(t), off)
   217  }
   218  
   219  func resolveTypeOff(ptrInModule unsafe.Pointer, off typeOff) *_type {
   220  	if off == 0 || off == -1 {
   221  		// -1 is the sentinel value for unreachable code.
   222  		// See cmd/link/internal/ld/data.go:relocsym.
   223  		return nil
   224  	}
   225  	base := uintptr(ptrInModule)
   226  	var md *moduledata
   227  	for next := &firstmoduledata; next != nil; next = next.next {
   228  		if base >= next.types && base < next.etypes {
   229  			md = next
   230  			break
   231  		}
   232  	}
   233  	if md == nil {
   234  		reflectOffsLock()
   235  		res := reflectOffs.m[int32(off)]
   236  		reflectOffsUnlock()
   237  		if res == nil {
   238  			println("runtime: typeOff", hex(off), "base", hex(base), "not in ranges:")
   239  			for next := &firstmoduledata; next != nil; next = next.next {
   240  				println("\ttypes", hex(next.types), "etypes", hex(next.etypes))
   241  			}
   242  			throw("runtime: type offset base pointer out of range")
   243  		}
   244  		return (*_type)(res)
   245  	}
   246  	if t := md.typemap[off]; t != nil {
   247  		return t
   248  	}
   249  	res := md.types + uintptr(off)
   250  	if res > md.etypes {
   251  		println("runtime: typeOff", hex(off), "out of range", hex(md.types), "-", hex(md.etypes))
   252  		throw("runtime: type offset out of range")
   253  	}
   254  	return (*_type)(unsafe.Pointer(res))
   255  }
   256  
   257  func (t *_type) typeOff(off typeOff) *_type {
   258  	return resolveTypeOff(unsafe.Pointer(t), off)
   259  }
   260  
   261  func (t *_type) textOff(off textOff) unsafe.Pointer {
   262  	if off == -1 {
   263  		// -1 is the sentinel value for unreachable code.
   264  		// See cmd/link/internal/ld/data.go:relocsym.
   265  		return unsafe.Pointer(^uintptr(0))
   266  	}
   267  	base := uintptr(unsafe.Pointer(t))
   268  	var md *moduledata
   269  	for next := &firstmoduledata; next != nil; next = next.next {
   270  		if base >= next.types && base < next.etypes {
   271  			md = next
   272  			break
   273  		}
   274  	}
   275  	if md == nil {
   276  		reflectOffsLock()
   277  		res := reflectOffs.m[int32(off)]
   278  		reflectOffsUnlock()
   279  		if res == nil {
   280  			println("runtime: textOff", hex(off), "base", hex(base), "not in ranges:")
   281  			for next := &firstmoduledata; next != nil; next = next.next {
   282  				println("\ttypes", hex(next.types), "etypes", hex(next.etypes))
   283  			}
   284  			throw("runtime: text offset base pointer out of range")
   285  		}
   286  		return res
   287  	}
   288  	res := uintptr(0)
   289  
   290  	// The text, or instruction stream is generated as one large buffer.  The off (offset) for a method is
   291  	// its offset within this buffer.  If the total text size gets too large, there can be issues on platforms like ppc64 if
   292  	// the target of calls are too far for the call instruction.  To resolve the large text issue, the text is split
   293  	// into multiple text sections to allow the linker to generate long calls when necessary.  When this happens, the vaddr
   294  	// for each text section is set to its offset within the text.  Each method's offset is compared against the section
   295  	// vaddrs and sizes to determine the containing section.  Then the section relative offset is added to the section's
   296  	// relocated baseaddr to compute the method addess.
   297  
   298  	if len(md.textsectmap) > 1 {
   299  		for i := range md.textsectmap {
   300  			sectaddr := md.textsectmap[i].vaddr
   301  			sectlen := md.textsectmap[i].length
   302  			if uintptr(off) >= sectaddr && uintptr(off) < sectaddr+sectlen {
   303  				res = md.textsectmap[i].baseaddr + uintptr(off) - uintptr(md.textsectmap[i].vaddr)
   304  				break
   305  			}
   306  		}
   307  	} else {
   308  		// single text section
   309  		res = md.text + uintptr(off)
   310  	}
   311  
   312  	if res > md.etext && GOARCH != "wasm" { // on wasm, functions do not live in the same address space as the linear memory
   313  		println("runtime: textOff", hex(off), "out of range", hex(md.text), "-", hex(md.etext))
   314  		throw("runtime: text offset out of range")
   315  	}
   316  	return unsafe.Pointer(res)
   317  }
   318  
   319  func (t *functype) in() []*_type {
   320  	// See funcType in reflect/type.go for details on data layout.
   321  	uadd := uintptr(unsafe.Sizeof(functype{}))
   322  	if t.typ.tflag&tflagUncommon != 0 {
   323  		uadd += unsafe.Sizeof(uncommontype{})
   324  	}
   325  	return (*[1 << 20]*_type)(add(unsafe.Pointer(t), uadd))[:t.inCount]
   326  }
   327  
   328  func (t *functype) out() []*_type {
   329  	// See funcType in reflect/type.go for details on data layout.
   330  	uadd := uintptr(unsafe.Sizeof(functype{}))
   331  	if t.typ.tflag&tflagUncommon != 0 {
   332  		uadd += unsafe.Sizeof(uncommontype{})
   333  	}
   334  	outCount := t.outCount & (1<<15 - 1)
   335  	return (*[1 << 20]*_type)(add(unsafe.Pointer(t), uadd))[t.inCount : t.inCount+outCount]
   336  }
   337  
   338  func (t *functype) dotdotdot() bool {
   339  	return t.outCount&(1<<15) != 0
   340  }
   341  
   342  type nameOff int32
   343  type typeOff int32
   344  type textOff int32
   345  
   346  type method struct {
   347  	name nameOff
   348  	mtyp typeOff
   349  	ifn  textOff
   350  	tfn  textOff
   351  }
   352  
   353  type uncommontype struct {
   354  	pkgpath nameOff
   355  	mcount  uint16 // number of methods
   356  	xcount  uint16 // number of exported methods
   357  	moff    uint32 // offset from this uncommontype to [mcount]method
   358  	_       uint32 // unused
   359  }
   360  
   361  type imethod struct {
   362  	name nameOff
   363  	ityp typeOff
   364  }
   365  
   366  type interfacetype struct {
   367  	typ     _type
   368  	pkgpath name
   369  	mhdr    []imethod
   370  }
   371  
   372  type maptype struct {
   373  	typ    _type
   374  	key    *_type
   375  	elem   *_type
   376  	bucket *_type // internal type representing a hash bucket
   377  	// function for hashing keys (ptr to key, seed) -> hash
   378  	hasher     func(unsafe.Pointer, uintptr) uintptr
   379  	keysize    uint8  // size of key slot
   380  	elemsize   uint8  // size of elem slot
   381  	bucketsize uint16 // size of bucket
   382  	flags      uint32
   383  }
   384  
   385  // Note: flag values must match those used in the TMAP case
   386  // in ../cmd/compile/internal/gc/reflect.go:dtypesym.
   387  func (mt *maptype) indirectkey() bool { // store ptr to key instead of key itself
   388  	return mt.flags&1 != 0
   389  }
   390  func (mt *maptype) indirectelem() bool { // store ptr to elem instead of elem itself
   391  	return mt.flags&2 != 0
   392  }
   393  func (mt *maptype) reflexivekey() bool { // true if k==k for all keys
   394  	return mt.flags&4 != 0
   395  }
   396  func (mt *maptype) needkeyupdate() bool { // true if we need to update key on an overwrite
   397  	return mt.flags&8 != 0
   398  }
   399  func (mt *maptype) hashMightPanic() bool { // true if hash function might panic
   400  	return mt.flags&16 != 0
   401  }
   402  
   403  type arraytype struct {
   404  	typ   _type
   405  	elem  *_type
   406  	slice *_type
   407  	len   uintptr
   408  }
   409  
   410  type chantype struct {
   411  	typ  _type
   412  	elem *_type
   413  	dir  uintptr
   414  }
   415  
   416  type slicetype struct {
   417  	typ  _type
   418  	elem *_type
   419  }
   420  
   421  type functype struct {
   422  	typ      _type
   423  	inCount  uint16
   424  	outCount uint16
   425  }
   426  
   427  type ptrtype struct {
   428  	typ  _type
   429  	elem *_type
   430  }
   431  
   432  type structfield struct {
   433  	name       name
   434  	typ        *_type
   435  	offsetAnon uintptr
   436  }
   437  
   438  func (f *structfield) offset() uintptr {
   439  	return f.offsetAnon >> 1
   440  }
   441  
   442  type structtype struct {
   443  	typ     _type
   444  	pkgPath name
   445  	fields  []structfield
   446  }
   447  
   448  // name is an encoded type name with optional extra data.
   449  // See reflect/type.go for details.
   450  type name struct {
   451  	bytes *byte
   452  }
   453  
   454  func (n name) data(off int) *byte {
   455  	return (*byte)(add(unsafe.Pointer(n.bytes), uintptr(off)))
   456  }
   457  
   458  func (n name) isExported() bool {
   459  	return (*n.bytes)&(1<<0) != 0
   460  }
   461  
   462  func (n name) nameLen() int {
   463  	return int(uint16(*n.data(1))<<8 | uint16(*n.data(2)))
   464  }
   465  
   466  func (n name) tagLen() int {
   467  	if *n.data(0)&(1<<1) == 0 {
   468  		return 0
   469  	}
   470  	off := 3 + n.nameLen()
   471  	return int(uint16(*n.data(off))<<8 | uint16(*n.data(off + 1)))
   472  }
   473  
   474  func (n name) name() (s string) {
   475  	if n.bytes == nil {
   476  		return ""
   477  	}
   478  	nl := n.nameLen()
   479  	if nl == 0 {
   480  		return ""
   481  	}
   482  	hdr := (*stringStruct)(unsafe.Pointer(&s))
   483  	hdr.str = unsafe.Pointer(n.data(3))
   484  	hdr.len = nl
   485  	return s
   486  }
   487  
   488  func (n name) tag() (s string) {
   489  	tl := n.tagLen()
   490  	if tl == 0 {
   491  		return ""
   492  	}
   493  	nl := n.nameLen()
   494  	hdr := (*stringStruct)(unsafe.Pointer(&s))
   495  	hdr.str = unsafe.Pointer(n.data(3 + nl + 2))
   496  	hdr.len = tl
   497  	return s
   498  }
   499  
   500  func (n name) pkgPath() string {
   501  	if n.bytes == nil || *n.data(0)&(1<<2) == 0 {
   502  		return ""
   503  	}
   504  	off := 3 + n.nameLen()
   505  	if tl := n.tagLen(); tl > 0 {
   506  		off += 2 + tl
   507  	}
   508  	var nameOff nameOff
   509  	copy((*[4]byte)(unsafe.Pointer(&nameOff))[:], (*[4]byte)(unsafe.Pointer(n.data(off)))[:])
   510  	pkgPathName := resolveNameOff(unsafe.Pointer(n.bytes), nameOff)
   511  	return pkgPathName.name()
   512  }
   513  
   514  func (n name) isBlank() bool {
   515  	if n.bytes == nil {
   516  		return false
   517  	}
   518  	if n.nameLen() != 1 {
   519  		return false
   520  	}
   521  	return *n.data(3) == '_'
   522  }
   523  
   524  // typelinksinit scans the types from extra modules and builds the
   525  // moduledata typemap used to de-duplicate type pointers.
   526  func typelinksinit() {
   527  	if firstmoduledata.next == nil {
   528  		return
   529  	}
   530  	typehash := make(map[uint32][]*_type, len(firstmoduledata.typelinks))
   531  
   532  	modules := activeModules()
   533  	prev := modules[0]
   534  	for _, md := range modules[1:] {
   535  		// Collect types from the previous module into typehash.
   536  	collect:
   537  		for _, tl := range prev.typelinks {
   538  			var t *_type
   539  			if prev.typemap == nil {
   540  				t = (*_type)(unsafe.Pointer(prev.types + uintptr(tl)))
   541  			} else {
   542  				t = prev.typemap[typeOff(tl)]
   543  			}
   544  			// Add to typehash if not seen before.
   545  			tlist := typehash[t.hash]
   546  			for _, tcur := range tlist {
   547  				if tcur == t {
   548  					continue collect
   549  				}
   550  			}
   551  			typehash[t.hash] = append(tlist, t)
   552  		}
   553  
   554  		if md.typemap == nil {
   555  			// If any of this module's typelinks match a type from a
   556  			// prior module, prefer that prior type by adding the offset
   557  			// to this module's typemap.
   558  			tm := make(map[typeOff]*_type, len(md.typelinks))
   559  			pinnedTypemaps = append(pinnedTypemaps, tm)
   560  			md.typemap = tm
   561  			for _, tl := range md.typelinks {
   562  				t := (*_type)(unsafe.Pointer(md.types + uintptr(tl)))
   563  				for _, candidate := range typehash[t.hash] {
   564  					seen := map[_typePair]struct{}{}
   565  					if typesEqual(t, candidate, seen) {
   566  						t = candidate
   567  						break
   568  					}
   569  				}
   570  				md.typemap[typeOff(tl)] = t
   571  			}
   572  		}
   573  
   574  		prev = md
   575  	}
   576  }
   577  
   578  type _typePair struct {
   579  	t1 *_type
   580  	t2 *_type
   581  }
   582  
   583  // typesEqual reports whether two types are equal.
   584  //
   585  // Everywhere in the runtime and reflect packages, it is assumed that
   586  // there is exactly one *_type per Go type, so that pointer equality
   587  // can be used to test if types are equal. There is one place that
   588  // breaks this assumption: buildmode=shared. In this case a type can
   589  // appear as two different pieces of memory. This is hidden from the
   590  // runtime and reflect package by the per-module typemap built in
   591  // typelinksinit. It uses typesEqual to map types from later modules
   592  // back into earlier ones.
   593  //
   594  // Only typelinksinit needs this function.
   595  func typesEqual(t, v *_type, seen map[_typePair]struct{}) bool {
   596  	tp := _typePair{t, v}
   597  	if _, ok := seen[tp]; ok {
   598  		return true
   599  	}
   600  
   601  	// mark these types as seen, and thus equivalent which prevents an infinite loop if
   602  	// the two types are identical, but recursively defined and loaded from
   603  	// different modules
   604  	seen[tp] = struct{}{}
   605  
   606  	if t == v {
   607  		return true
   608  	}
   609  	kind := t.kind & kindMask
   610  	if kind != v.kind&kindMask {
   611  		return false
   612  	}
   613  	if t.string() != v.string() {
   614  		return false
   615  	}
   616  	ut := t.uncommon()
   617  	uv := v.uncommon()
   618  	if ut != nil || uv != nil {
   619  		if ut == nil || uv == nil {
   620  			return false
   621  		}
   622  		pkgpatht := t.nameOff(ut.pkgpath).name()
   623  		pkgpathv := v.nameOff(uv.pkgpath).name()
   624  		if pkgpatht != pkgpathv {
   625  			return false
   626  		}
   627  	}
   628  	if kindBool <= kind && kind <= kindComplex128 {
   629  		return true
   630  	}
   631  	switch kind {
   632  	case kindString, kindUnsafePointer:
   633  		return true
   634  	case kindArray:
   635  		at := (*arraytype)(unsafe.Pointer(t))
   636  		av := (*arraytype)(unsafe.Pointer(v))
   637  		return typesEqual(at.elem, av.elem, seen) && at.len == av.len
   638  	case kindChan:
   639  		ct := (*chantype)(unsafe.Pointer(t))
   640  		cv := (*chantype)(unsafe.Pointer(v))
   641  		return ct.dir == cv.dir && typesEqual(ct.elem, cv.elem, seen)
   642  	case kindFunc:
   643  		ft := (*functype)(unsafe.Pointer(t))
   644  		fv := (*functype)(unsafe.Pointer(v))
   645  		if ft.outCount != fv.outCount || ft.inCount != fv.inCount {
   646  			return false
   647  		}
   648  		tin, vin := ft.in(), fv.in()
   649  		for i := 0; i < len(tin); i++ {
   650  			if !typesEqual(tin[i], vin[i], seen) {
   651  				return false
   652  			}
   653  		}
   654  		tout, vout := ft.out(), fv.out()
   655  		for i := 0; i < len(tout); i++ {
   656  			if !typesEqual(tout[i], vout[i], seen) {
   657  				return false
   658  			}
   659  		}
   660  		return true
   661  	case kindInterface:
   662  		it := (*interfacetype)(unsafe.Pointer(t))
   663  		iv := (*interfacetype)(unsafe.Pointer(v))
   664  		if it.pkgpath.name() != iv.pkgpath.name() {
   665  			return false
   666  		}
   667  		if len(it.mhdr) != len(iv.mhdr) {
   668  			return false
   669  		}
   670  		for i := range it.mhdr {
   671  			tm := &it.mhdr[i]
   672  			vm := &iv.mhdr[i]
   673  			// Note the mhdr array can be relocated from
   674  			// another module. See #17724.
   675  			tname := resolveNameOff(unsafe.Pointer(tm), tm.name)
   676  			vname := resolveNameOff(unsafe.Pointer(vm), vm.name)
   677  			if tname.name() != vname.name() {
   678  				return false
   679  			}
   680  			if tname.pkgPath() != vname.pkgPath() {
   681  				return false
   682  			}
   683  			tityp := resolveTypeOff(unsafe.Pointer(tm), tm.ityp)
   684  			vityp := resolveTypeOff(unsafe.Pointer(vm), vm.ityp)
   685  			if !typesEqual(tityp, vityp, seen) {
   686  				return false
   687  			}
   688  		}
   689  		return true
   690  	case kindMap:
   691  		mt := (*maptype)(unsafe.Pointer(t))
   692  		mv := (*maptype)(unsafe.Pointer(v))
   693  		return typesEqual(mt.key, mv.key, seen) && typesEqual(mt.elem, mv.elem, seen)
   694  	case kindPtr:
   695  		pt := (*ptrtype)(unsafe.Pointer(t))
   696  		pv := (*ptrtype)(unsafe.Pointer(v))
   697  		return typesEqual(pt.elem, pv.elem, seen)
   698  	case kindSlice:
   699  		st := (*slicetype)(unsafe.Pointer(t))
   700  		sv := (*slicetype)(unsafe.Pointer(v))
   701  		return typesEqual(st.elem, sv.elem, seen)
   702  	case kindStruct:
   703  		st := (*structtype)(unsafe.Pointer(t))
   704  		sv := (*structtype)(unsafe.Pointer(v))
   705  		if len(st.fields) != len(sv.fields) {
   706  			return false
   707  		}
   708  		if st.pkgPath.name() != sv.pkgPath.name() {
   709  			return false
   710  		}
   711  		for i := range st.fields {
   712  			tf := &st.fields[i]
   713  			vf := &sv.fields[i]
   714  			if tf.name.name() != vf.name.name() {
   715  				return false
   716  			}
   717  			if !typesEqual(tf.typ, vf.typ, seen) {
   718  				return false
   719  			}
   720  			if tf.name.tag() != vf.name.tag() {
   721  				return false
   722  			}
   723  			if tf.offsetAnon != vf.offsetAnon {
   724  				return false
   725  			}
   726  		}
   727  		return true
   728  	default:
   729  		println("runtime: impossible type kind", kind)
   730  		throw("runtime: impossible type kind")
   731  		return false
   732  	}
   733  }
   734  

View as plain text