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  
    62  	// Expand frame type's GC bitmap into byte-map.
    63  	ptrs = ft.Pointers()
    64  	if ptrs {
    65  		nptrs := ft.PtrBytes / goarch.PtrSize
    66  		gcdata := ft.GcSlice(0, (nptrs+7)/8)
    67  		for i := uintptr(0); i < nptrs; i++ {
    68  			gc = append(gc, gcdata[i/8]>>(i%8)&1)
    69  		}
    70  	}
    71  	return
    72  }
    73  
    74  func TypeLinks() []string {
    75  	first, rest := compiledTypelinks()
    76  
    77  	var r []string
    78  
    79  	addTypes := func(types []*abi.Type) {
    80  		for _, typ := range types {
    81  			r = append(r, stringFor(typ))
    82  		}
    83  	}
    84  
    85  	addTypes(first)
    86  	for _, rt := range rest {
    87  		addTypes(rt)
    88  	}
    89  
    90  	return r
    91  }
    92  
    93  var GCBits = gcbits
    94  
    95  func gcbits(any) []byte // provided by runtime
    96  
    97  type EmbedWithUnexpMeth struct{}
    98  
    99  func (EmbedWithUnexpMeth) f() {}
   100  
   101  type pinUnexpMeth interface {
   102  	f()
   103  }
   104  
   105  var pinUnexpMethI = pinUnexpMeth(EmbedWithUnexpMeth{})
   106  
   107  func FirstMethodNameBytes(t Type) *byte {
   108  	_ = pinUnexpMethI
   109  
   110  	ut := t.uncommon()
   111  	if ut == nil {
   112  		panic("type has no methods")
   113  	}
   114  	m := ut.Methods()[0]
   115  	mname := t.(*rtype).nameOff(m.Name)
   116  	if *mname.DataChecked(0, "name flag field")&(1<<2) == 0 {
   117  		panic("method name does not have pkgPath *string")
   118  	}
   119  	return mname.Bytes
   120  }
   121  
   122  type OtherPkgFields struct {
   123  	OtherExported   int
   124  	otherUnexported int
   125  }
   126  
   127  func IsExported(t Type) bool {
   128  	typ := t.(*rtype)
   129  	n := typ.nameOff(typ.t.Str)
   130  	return n.IsExported()
   131  }
   132  
   133  func ResolveReflectName(s string) {
   134  	resolveReflectName(newName(s, "", false, false))
   135  }
   136  
   137  type Buffer struct {
   138  	buf []byte
   139  }
   140  
   141  func clearLayoutCache() {
   142  	layoutCache = sync.Map{}
   143  }
   144  
   145  func SetArgRegs(ints, floats int, floatSize uintptr) (oldInts, oldFloats int, oldFloatSize uintptr) {
   146  	oldInts = intArgRegs
   147  	oldFloats = floatArgRegs
   148  	oldFloatSize = floatRegSize
   149  	intArgRegs = ints
   150  	floatArgRegs = floats
   151  	floatRegSize = floatSize
   152  	clearLayoutCache()
   153  	return
   154  }
   155  
   156  var MethodValueCallCodePtr = methodValueCallCodePtr
   157  
   158  var InternalIsZero = isZero
   159  
   160  var IsRegularMemory = isRegularMemory
   161  
   162  func MapGroupOf(x, y Type) Type {
   163  	return groupOf(x, y)
   164  }
   165  

View as plain text