github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/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 "unsafe" 8 9 // MakeRO returns a copy of v with the read-only flag set. 10 func MakeRO(v Value) Value { 11 v.flag |= flagStickyRO 12 return v 13 } 14 15 // IsRO reports whether v's read-only flag is set. 16 func IsRO(v Value) bool { 17 return v.flag&flagStickyRO != 0 18 } 19 20 var CallGC = &callGC 21 22 const PtrSize = ptrSize 23 24 func FuncLayout(t Type, rcvr Type) (frametype Type, argSize, retOffset uintptr, stack []byte, gc []byte, ptrs bool) { 25 var ft *rtype 26 var s *bitVector 27 if rcvr != nil { 28 ft, argSize, retOffset, s, _ = funcLayout((*funcType)(unsafe.Pointer(t.(*rtype))), rcvr.(*rtype)) 29 } else { 30 ft, argSize, retOffset, s, _ = funcLayout((*funcType)(unsafe.Pointer(t.(*rtype))), nil) 31 } 32 frametype = ft 33 for i := uint32(0); i < s.n; i++ { 34 stack = append(stack, s.data[i/8]>>(i%8)&1) 35 } 36 if ft.kind&kindGCProg != 0 { 37 panic("can't handle gc programs") 38 } 39 ptrs = ft.ptrdata != 0 40 if ptrs { 41 nptrs := ft.ptrdata / ptrSize 42 gcdata := ft.gcSlice(0, (nptrs+7)/8) 43 for i := uintptr(0); i < nptrs; i++ { 44 gc = append(gc, gcdata[i/8]>>(i%8)&1) 45 } 46 } 47 return 48 } 49 50 func TypeLinks() []string { 51 var r []string 52 sections, offset := typelinks() 53 for i, offs := range offset { 54 rodata := sections[i] 55 for _, off := range offs { 56 typ := (*rtype)(resolveTypeOff(unsafe.Pointer(rodata), off)) 57 r = append(r, typ.String()) 58 } 59 } 60 return r 61 } 62 63 var GCBits = gcbits 64 65 func gcbits(interface{}) []byte // provided by runtime 66 67 func MapBucketOf(x, y Type) Type { 68 return bucketOf(x.(*rtype), y.(*rtype)) 69 } 70 71 func CachedBucketOf(m Type) Type { 72 t := m.(*rtype) 73 if Kind(t.kind&kindMask) != Map { 74 panic("not map") 75 } 76 tt := (*mapType)(unsafe.Pointer(t)) 77 return tt.bucket 78 } 79 80 type EmbedWithUnexpMeth struct{} 81 82 func (EmbedWithUnexpMeth) f() {} 83 84 type pinUnexpMeth interface { 85 f() 86 } 87 88 var pinUnexpMethI = pinUnexpMeth(EmbedWithUnexpMeth{}) 89 90 func FirstMethodNameBytes(t Type) *byte { 91 _ = pinUnexpMethI 92 93 ut := t.uncommon() 94 if ut == nil { 95 panic("type has no methods") 96 } 97 m := ut.methods()[0] 98 mname := t.(*rtype).nameOff(m.name) 99 if *mname.data(0, "name flag field")&(1<<2) == 0 { 100 panic("method name does not have pkgPath *string") 101 } 102 return mname.bytes 103 } 104 105 type OtherPkgFields struct { 106 OtherExported int 107 otherUnexported int 108 } 109 110 func IsExported(t Type) bool { 111 typ := t.(*rtype) 112 n := typ.nameOff(typ.str) 113 return n.isExported() 114 } 115 116 func ResolveReflectName(s string) { 117 resolveReflectName(newName(s, "", false)) 118 } 119 120 type Buffer struct { 121 buf []byte 122 }