github.com/mtsmfm/go/src@v0.0.0-20221020090648-44bdcb9f8fde/reflect/export_test.go (about)

     1  // Copyright 2012 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 reflect
     6  
     7  import (
     8  	"internal/goarch"
     9  	"sync"
    10  	"unsafe"
    11  )
    12  
    13  // MakeRO returns a copy of v with the read-only flag set.
    14  func MakeRO(v Value) Value {
    15  	v.flag |= flagStickyRO
    16  	return v
    17  }
    18  
    19  // IsRO reports whether v's read-only flag is set.
    20  func IsRO(v Value) bool {
    21  	return v.flag&flagStickyRO != 0
    22  }
    23  
    24  var CallGC = &callGC
    25  
    26  // FuncLayout calls funcLayout and returns a subset of the results for testing.
    27  //
    28  // Bitmaps like stack, gc, inReg, and outReg are expanded such that each bit
    29  // takes up one byte, so that writing out test cases is a little clearer.
    30  // If ptrs is false, gc will be nil.
    31  func FuncLayout(t Type, rcvr Type) (frametype Type, argSize, retOffset uintptr, stack, gc, inReg, outReg []byte, ptrs bool) {
    32  	var ft *rtype
    33  	var abid abiDesc
    34  	if rcvr != nil {
    35  		ft, _, abid = funcLayout((*funcType)(unsafe.Pointer(t.(*rtype))), rcvr.(*rtype))
    36  	} else {
    37  		ft, _, abid = funcLayout((*funcType)(unsafe.Pointer(t.(*rtype))), nil)
    38  	}
    39  	// Extract size information.
    40  	argSize = abid.stackCallArgsSize
    41  	retOffset = abid.retOffset
    42  	frametype = ft
    43  
    44  	// Expand stack pointer bitmap into byte-map.
    45  	for i := uint32(0); i < abid.stackPtrs.n; i++ {
    46  		stack = append(stack, abid.stackPtrs.data[i/8]>>(i%8)&1)
    47  	}
    48  
    49  	// Expand register pointer bitmaps into byte-maps.
    50  	bool2byte := func(b bool) byte {
    51  		if b {
    52  			return 1
    53  		}
    54  		return 0
    55  	}
    56  	for i := 0; i < intArgRegs; i++ {
    57  		inReg = append(inReg, bool2byte(abid.inRegPtrs.Get(i)))
    58  		outReg = append(outReg, bool2byte(abid.outRegPtrs.Get(i)))
    59  	}
    60  	if ft.kind&kindGCProg != 0 {
    61  		panic("can't handle gc programs")
    62  	}
    63  
    64  	// Expand frame type's GC bitmap into byte-map.
    65  	ptrs = ft.ptrdata != 0
    66  	if ptrs {
    67  		nptrs := ft.ptrdata / goarch.PtrSize
    68  		gcdata := ft.gcSlice(0, (nptrs+7)/8)
    69  		for i := uintptr(0); i < nptrs; i++ {
    70  			gc = append(gc, gcdata[i/8]>>(i%8)&1)
    71  		}
    72  	}
    73  	return
    74  }
    75  
    76  func TypeLinks() []string {
    77  	var r []string
    78  	sections, offset := typelinks()
    79  	for i, offs := range offset {
    80  		rodata := sections[i]
    81  		for _, off := range offs {
    82  			typ := (*rtype)(resolveTypeOff(unsafe.Pointer(rodata), off))
    83  			r = append(r, typ.String())
    84  		}
    85  	}
    86  	return r
    87  }
    88  
    89  var GCBits = gcbits
    90  
    91  func gcbits(any) []byte // provided by runtime
    92  
    93  func MapBucketOf(x, y Type) Type {
    94  	return bucketOf(x.(*rtype), y.(*rtype))
    95  }
    96  
    97  func CachedBucketOf(m Type) Type {
    98  	t := m.(*rtype)
    99  	if Kind(t.kind&kindMask) != Map {
   100  		panic("not map")
   101  	}
   102  	tt := (*mapType)(unsafe.Pointer(t))
   103  	return tt.bucket
   104  }
   105  
   106  type EmbedWithUnexpMeth struct{}
   107  
   108  func (EmbedWithUnexpMeth) f() {}
   109  
   110  type pinUnexpMeth interface {
   111  	f()
   112  }
   113  
   114  var pinUnexpMethI = pinUnexpMeth(EmbedWithUnexpMeth{})
   115  
   116  func FirstMethodNameBytes(t Type) *byte {
   117  	_ = pinUnexpMethI
   118  
   119  	ut := t.uncommon()
   120  	if ut == nil {
   121  		panic("type has no methods")
   122  	}
   123  	m := ut.methods()[0]
   124  	mname := t.(*rtype).nameOff(m.name)
   125  	if *mname.data(0, "name flag field")&(1<<2) == 0 {
   126  		panic("method name does not have pkgPath *string")
   127  	}
   128  	return mname.bytes
   129  }
   130  
   131  type OtherPkgFields struct {
   132  	OtherExported   int
   133  	otherUnexported int
   134  }
   135  
   136  func IsExported(t Type) bool {
   137  	typ := t.(*rtype)
   138  	n := typ.nameOff(typ.str)
   139  	return n.isExported()
   140  }
   141  
   142  func ResolveReflectName(s string) {
   143  	resolveReflectName(newName(s, "", false, false))
   144  }
   145  
   146  type Buffer struct {
   147  	buf []byte
   148  }
   149  
   150  func clearLayoutCache() {
   151  	layoutCache = sync.Map{}
   152  }
   153  
   154  func SetArgRegs(ints, floats int, floatSize uintptr) (oldInts, oldFloats int, oldFloatSize uintptr) {
   155  	oldInts = intArgRegs
   156  	oldFloats = floatArgRegs
   157  	oldFloatSize = floatRegSize
   158  	intArgRegs = ints
   159  	floatArgRegs = floats
   160  	floatRegSize = floatSize
   161  	clearLayoutCache()
   162  	return
   163  }
   164  
   165  var MethodValueCallCodePtr = methodValueCallCodePtr