...

Source file src/reflect/export_test.go

Documentation: reflect

		 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  	"sync"
		 9  	"unsafe"
		10  )
		11  
		12  // MakeRO returns a copy of v with the read-only flag set.
		13  func MakeRO(v Value) Value {
		14  	v.flag |= flagStickyRO
		15  	return v
		16  }
		17  
		18  // IsRO reports whether v's read-only flag is set.
		19  func IsRO(v Value) bool {
		20  	return v.flag&flagStickyRO != 0
		21  }
		22  
		23  var CallGC = &callGC
		24  
		25  const PtrSize = ptrSize
		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 *rtype
		34  	var abid abiDesc
		35  	if rcvr != nil {
		36  		ft, _, abid = funcLayout((*funcType)(unsafe.Pointer(t.(*rtype))), rcvr.(*rtype))
		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 = 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&kindGCProg != 0 {
		62  		panic("can't handle gc programs")
		63  	}
		64  
		65  	// Expand frame type's GC bitmap into byte-map.
		66  	ptrs = ft.ptrdata != 0
		67  	if ptrs {
		68  		nptrs := ft.ptrdata / 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(unsafe.Pointer(rodata), off))
		84  			r = append(r, typ.String())
		85  		}
		86  	}
		87  	return r
		88  }
		89  
		90  var GCBits = gcbits
		91  
		92  func gcbits(interface{}) []byte // provided by runtime
		93  
		94  func MapBucketOf(x, y Type) Type {
		95  	return bucketOf(x.(*rtype), y.(*rtype))
		96  }
		97  
		98  func CachedBucketOf(m Type) Type {
		99  	t := m.(*rtype)
	 100  	if Kind(t.kind&kindMask) != Map {
	 101  		panic("not map")
	 102  	}
	 103  	tt := (*mapType)(unsafe.Pointer(t))
	 104  	return tt.bucket
	 105  }
	 106  
	 107  type EmbedWithUnexpMeth struct{}
	 108  
	 109  func (EmbedWithUnexpMeth) f() {}
	 110  
	 111  type pinUnexpMeth interface {
	 112  	f()
	 113  }
	 114  
	 115  var pinUnexpMethI = pinUnexpMeth(EmbedWithUnexpMeth{})
	 116  
	 117  func FirstMethodNameBytes(t Type) *byte {
	 118  	_ = pinUnexpMethI
	 119  
	 120  	ut := t.uncommon()
	 121  	if ut == nil {
	 122  		panic("type has no methods")
	 123  	}
	 124  	m := ut.methods()[0]
	 125  	mname := t.(*rtype).nameOff(m.name)
	 126  	if *mname.data(0, "name flag field")&(1<<2) == 0 {
	 127  		panic("method name does not have pkgPath *string")
	 128  	}
	 129  	return mname.bytes
	 130  }
	 131  
	 132  type OtherPkgFields struct {
	 133  	OtherExported	 int
	 134  	otherUnexported int
	 135  }
	 136  
	 137  func IsExported(t Type) bool {
	 138  	typ := t.(*rtype)
	 139  	n := typ.nameOff(typ.str)
	 140  	return n.isExported()
	 141  }
	 142  
	 143  func ResolveReflectName(s string) {
	 144  	resolveReflectName(newName(s, "", false))
	 145  }
	 146  
	 147  type Buffer struct {
	 148  	buf []byte
	 149  }
	 150  
	 151  func clearLayoutCache() {
	 152  	layoutCache = sync.Map{}
	 153  }
	 154  
	 155  func SetArgRegs(ints, floats int, floatSize uintptr) (oldInts, oldFloats int, oldFloatSize uintptr) {
	 156  	oldInts = intArgRegs
	 157  	oldFloats = floatArgRegs
	 158  	oldFloatSize = floatRegSize
	 159  	intArgRegs = ints
	 160  	floatArgRegs = floats
	 161  	floatRegSize = floatSize
	 162  	clearLayoutCache()
	 163  	return
	 164  }
	 165  

View as plain text