github.com/goplusjs/reflectx@v0.5.4/value.go (about) 1 // Copyright 2009 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 implements run-time reflection, allowing a program to 6 // manipulate objects with arbitrary types. The typical use is to take a value 7 // with static type interface{} and extract its dynamic type information by 8 // calling TypeOf, which returns a Type. 9 // 10 // A call to ValueOf returns a Value representing the run-time data. 11 // Zero takes a Type and returns a Value representing a zero value 12 // for that type. 13 // 14 // See "The Laws of Reflection" for an introduction to reflection in Go: 15 // https://golang.org/doc/articles/laws_of_reflection.html 16 17 package reflectx 18 19 import ( 20 "unsafe" 21 ) 22 23 type Value struct { 24 // typ holds the type of the value represented by a Value. 25 typ *rtype 26 27 // Pointer-valued data or, if flagIndir is set, pointer to data. 28 // Valid when either flagIndir is set or typ.pointers() is true. 29 ptr unsafe.Pointer 30 31 // flag holds metadata about the value. 32 // The lowest bits are flag bits: 33 // - flagStickyRO: obtained via unexported not embedded field, so read-only 34 // - flagEmbedRO: obtained via unexported embedded field, so read-only 35 // - flagIndir: val holds a pointer to the data 36 // - flagAddr: v.CanAddr is true (implies flagIndir) 37 // - flagMethod: v is a method value. 38 // The next five bits give the Kind of the value. 39 // This repeats typ.Kind() except for method values. 40 // The remaining 23+ bits give a method number for method values. 41 // If flag.kind() != Func, code can assume that flagMethod is unset. 42 // If ifaceIndir(typ), code can assume that flagIndir is set. 43 flag 44 45 // A method value represents a curried method invocation 46 // like r.Read for some receiver r. The typ+val+flag bits describe 47 // the receiver r, but the flag's Kind bits say Func (methods are 48 // functions), and the top bits of the flag give the method number 49 // in r's type's method table. 50 } 51 52 type flag uintptr 53 54 const ( 55 flagKindWidth = 5 // there are 27 kinds 56 flagKindMask flag = 1<<flagKindWidth - 1 57 flagStickyRO flag = 1 << 5 58 flagEmbedRO flag = 1 << 6 59 flagIndir flag = 1 << 7 60 flagAddr flag = 1 << 8 61 flagMethod flag = 1 << 9 62 flagMethodShift = 10 63 flagRO flag = flagStickyRO | flagEmbedRO 64 )