Source file src/reflect/export_test.go

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

View as plain text