github.com/zxy12/go_duplicate_112_new@v0.0.0-20200807091221-747231827200/src/reflect/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 6 7 import ( 8 "math" 9 "runtime" 10 "unsafe" 11 ) 12 13 const ptrSize = 4 << (^uintptr(0) >> 63) // unsafe.Sizeof(uintptr(0)) but an ideal const 14 15 // Value is the reflection interface to a Go value. 16 // 17 // Not all methods apply to all kinds of values. Restrictions, 18 // if any, are noted in the documentation for each method. 19 // Use the Kind method to find out the kind of value before 20 // calling kind-specific methods. Calling a method 21 // inappropriate to the kind of type causes a run time panic. 22 // 23 // The zero Value represents no value. 24 // Its IsValid method returns false, its Kind method returns Invalid, 25 // its String method returns "<invalid Value>", and all other methods panic. 26 // Most functions and methods never return an invalid value. 27 // If one does, its documentation states the conditions explicitly. 28 // 29 // A Value can be used concurrently by multiple goroutines provided that 30 // the underlying Go value can be used concurrently for the equivalent 31 // direct operations. 32 // 33 // To compare two Values, compare the results of the Interface method. 34 // Using == on two Values does not compare the underlying values 35 // they represent. 36 type Value struct { 37 // typ holds the type of the value represented by a Value. 38 typ *rtype 39 40 // Pointer-valued data or, if flagIndir is set, pointer to data. 41 // Valid when either flagIndir is set or typ.pointers() is true. 42 ptr unsafe.Pointer 43 44 // flag holds metadata about the value. 45 // The lowest bits are flag bits: 46 // - flagStickyRO: obtained via unexported not embedded field, so read-only 47 // - flagEmbedRO: obtained via unexported embedded field, so read-only 48 // - flagIndir: val holds a pointer to the data 49 // - flagAddr: v.CanAddr is true (implies flagIndir) 50 // - flagMethod: v is a method value. 51 // The next five bits give the Kind of the value. 52 // This repeats typ.Kind() except for method values. 53 // The remaining 23+ bits give a method number for method values. 54 // If flag.kind() != Func, code can assume that flagMethod is unset. 55 // If ifaceIndir(typ), code can assume that flagIndir is set. 56 flag 57 58 // A method value represents a curried method invocation 59 // like r.Read for some receiver r. The typ+val+flag bits describe 60 // the receiver r, but the flag's Kind bits say Func (methods are 61 // functions), and the top bits of the flag give the method number 62 // in r's type's method table. 63 } 64 65 type flag uintptr 66 67 const ( 68 flagKindWidth = 5 // there are 27 kinds 69 flagKindMask flag = 1<<flagKindWidth - 1 70 flagStickyRO flag = 1 << 5 71 flagEmbedRO flag = 1 << 6 72 flagIndir flag = 1 << 7 73 flagAddr flag = 1 << 8 74 flagMethod flag = 1 << 9 75 flagMethodShift = 10 76 flagRO flag = flagStickyRO | flagEmbedRO 77 ) 78 79 func (f flag) kind() Kind { 80 return Kind(f & flagKindMask) 81 } 82 83 func (f flag) ro() flag { 84 if f&flagRO != 0 { 85 return flagStickyRO 86 } 87 return 0 88 } 89 90 // pointer returns the underlying pointer represented by v. 91 // v.Kind() must be Ptr, Map, Chan, Func, or UnsafePointer 92 func (v Value) pointer() unsafe.Pointer { 93 if v.typ.size != ptrSize || !v.typ.pointers() { 94 panic("can't call pointer on a non-pointer Value") 95 } 96 if v.flag&flagIndir != 0 { 97 return *(*unsafe.Pointer)(v.ptr) 98 } 99 return v.ptr 100 } 101 102 // packEface converts v to the empty interface. 103 func packEface(v Value) interface{} { 104 t := v.typ 105 var i interface{} 106 e := (*emptyInterface)(unsafe.Pointer(&i)) 107 // First, fill in the data portion of the interface. 108 switch { 109 case ifaceIndir(t): 110 if v.flag&flagIndir == 0 { 111 panic("bad indir") 112 } 113 // Value is indirect, and so is the interface we're making. 114 ptr := v.ptr 115 if v.flag&flagAddr != 0 { 116 // TODO: pass safe boolean from valueInterface so 117 // we don't need to copy if safe==true? 118 c := unsafe_New(t) 119 typedmemmove(t, c, ptr) 120 ptr = c 121 } 122 e.word = ptr 123 case v.flag&flagIndir != 0: 124 // Value is indirect, but interface is direct. We need 125 // to load the data at v.ptr into the interface data word. 126 e.word = *(*unsafe.Pointer)(v.ptr) 127 default: 128 // Value is direct, and so is the interface. 129 e.word = v.ptr 130 } 131 // Now, fill in the type portion. We're very careful here not 132 // to have any operation between the e.word and e.typ assignments 133 // that would let the garbage collector observe the partially-built 134 // interface value. 135 e.typ = t 136 return i 137 } 138 139 // unpackEface converts the empty interface i to a Value. 140 func unpackEface(i interface{}) Value { 141 e := (*emptyInterface)(unsafe.Pointer(&i)) 142 // NOTE: don't read e.word until we know whether it is really a pointer or not. 143 t := e.typ 144 if t == nil { 145 return Value{} 146 } 147 f := flag(t.Kind()) 148 if ifaceIndir(t) { 149 f |= flagIndir 150 } 151 return Value{t, e.word, f} 152 } 153 154 // A ValueError occurs when a Value method is invoked on 155 // a Value that does not support it. Such cases are documented 156 // in the description of each method. 157 type ValueError struct { 158 Method string 159 Kind Kind 160 } 161 162 func (e *ValueError) Error() string { 163 if e.Kind == 0 { 164 return "reflect: call of " + e.Method + " on zero Value" 165 } 166 return "reflect: call of " + e.Method + " on " + e.Kind.String() + " Value" 167 } 168 169 // methodName returns the name of the calling method, 170 // assumed to be two stack frames above. 171 func methodName() string { 172 pc, _, _, _ := runtime.Caller(2) 173 f := runtime.FuncForPC(pc) 174 if f == nil { 175 return "unknown method" 176 } 177 return f.Name() 178 } 179 180 // emptyInterface is the header for an interface{} value. 181 type emptyInterface struct { 182 typ *rtype 183 word unsafe.Pointer 184 } 185 186 // nonEmptyInterface is the header for an interface value with methods. 187 type nonEmptyInterface struct { 188 // see ../runtime/iface.go:/Itab 189 itab *struct { 190 ityp *rtype // static interface type 191 typ *rtype // dynamic concrete type 192 hash uint32 // copy of typ.hash 193 _ [4]byte 194 fun [100000]unsafe.Pointer // method table 195 } 196 word unsafe.Pointer 197 } 198 199 // mustBe panics if f's kind is not expected. 200 // Making this a method on flag instead of on Value 201 // (and embedding flag in Value) means that we can write 202 // the very clear v.mustBe(Bool) and have it compile into 203 // v.flag.mustBe(Bool), which will only bother to copy the 204 // single important word for the receiver. 205 func (f flag) mustBe(expected Kind) { 206 if f.kind() != expected { 207 panic(&ValueError{methodName(), f.kind()}) 208 } 209 } 210 211 // mustBeExported panics if f records that the value was obtained using 212 // an unexported field. 213 func (f flag) mustBeExported() { 214 if f == 0 { 215 panic(&ValueError{methodName(), 0}) 216 } 217 if f&flagRO != 0 { 218 panic("reflect: " + methodName() + " using value obtained using unexported field") 219 } 220 } 221 222 // mustBeAssignable panics if f records that the value is not assignable, 223 // which is to say that either it was obtained using an unexported field 224 // or it is not addressable. 225 func (f flag) mustBeAssignable() { 226 if f == 0 { 227 panic(&ValueError{methodName(), Invalid}) 228 } 229 // Assignable if addressable and not read-only. 230 if f&flagRO != 0 { 231 panic("reflect: " + methodName() + " using value obtained using unexported field") 232 } 233 if f&flagAddr == 0 { 234 panic("reflect: " + methodName() + " using unaddressable value") 235 } 236 } 237 238 // Addr returns a pointer value representing the address of v. 239 // It panics if CanAddr() returns false. 240 // Addr is typically used to obtain a pointer to a struct field 241 // or slice element in order to call a method that requires a 242 // pointer receiver. 243 func (v Value) Addr() Value { 244 if v.flag&flagAddr == 0 { 245 panic("reflect.Value.Addr of unaddressable value") 246 } 247 return Value{v.typ.ptrTo(), v.ptr, v.flag.ro() | flag(Ptr)} 248 } 249 250 // Bool returns v's underlying value. 251 // It panics if v's kind is not Bool. 252 func (v Value) Bool() bool { 253 v.mustBe(Bool) 254 return *(*bool)(v.ptr) 255 } 256 257 // Bytes returns v's underlying value. 258 // It panics if v's underlying value is not a slice of bytes. 259 func (v Value) Bytes() []byte { 260 v.mustBe(Slice) 261 if v.typ.Elem().Kind() != Uint8 { 262 panic("reflect.Value.Bytes of non-byte slice") 263 } 264 // Slice is always bigger than a word; assume flagIndir. 265 return *(*[]byte)(v.ptr) 266 } 267 268 // runes returns v's underlying value. 269 // It panics if v's underlying value is not a slice of runes (int32s). 270 func (v Value) runes() []rune { 271 v.mustBe(Slice) 272 if v.typ.Elem().Kind() != Int32 { 273 panic("reflect.Value.Bytes of non-rune slice") 274 } 275 // Slice is always bigger than a word; assume flagIndir. 276 return *(*[]rune)(v.ptr) 277 } 278 279 // CanAddr reports whether the value's address can be obtained with Addr. 280 // Such values are called addressable. A value is addressable if it is 281 // an element of a slice, an element of an addressable array, 282 // a field of an addressable struct, or the result of dereferencing a pointer. 283 // If CanAddr returns false, calling Addr will panic. 284 func (v Value) CanAddr() bool { 285 return v.flag&flagAddr != 0 286 } 287 288 // CanSet reports whether the value of v can be changed. 289 // A Value can be changed only if it is addressable and was not 290 // obtained by the use of unexported struct fields. 291 // If CanSet returns false, calling Set or any type-specific 292 // setter (e.g., SetBool, SetInt) will panic. 293 func (v Value) CanSet() bool { 294 return v.flag&(flagAddr|flagRO) == flagAddr 295 } 296 297 // Call calls the function v with the input arguments in. 298 // For example, if len(in) == 3, v.Call(in) represents the Go call v(in[0], in[1], in[2]). 299 // Call panics if v's Kind is not Func. 300 // It returns the output results as Values. 301 // As in Go, each input argument must be assignable to the 302 // type of the function's corresponding input parameter. 303 // If v is a variadic function, Call creates the variadic slice parameter 304 // itself, copying in the corresponding values. 305 func (v Value) Call(in []Value) []Value { 306 v.mustBe(Func) 307 v.mustBeExported() 308 return v.call("Call", in) 309 } 310 311 // CallSlice calls the variadic function v with the input arguments in, 312 // assigning the slice in[len(in)-1] to v's final variadic argument. 313 // For example, if len(in) == 3, v.CallSlice(in) represents the Go call v(in[0], in[1], in[2]...). 314 // CallSlice panics if v's Kind is not Func or if v is not variadic. 315 // It returns the output results as Values. 316 // As in Go, each input argument must be assignable to the 317 // type of the function's corresponding input parameter. 318 func (v Value) CallSlice(in []Value) []Value { 319 v.mustBe(Func) 320 v.mustBeExported() 321 return v.call("CallSlice", in) 322 } 323 324 var callGC bool // for testing; see TestCallMethodJump 325 326 func (v Value) call(op string, in []Value) []Value { 327 // Get function pointer, type. 328 t := (*funcType)(unsafe.Pointer(v.typ)) 329 var ( 330 fn unsafe.Pointer 331 rcvr Value 332 rcvrtype *rtype 333 ) 334 if v.flag&flagMethod != 0 { 335 rcvr = v 336 rcvrtype, t, fn = methodReceiver(op, v, int(v.flag)>>flagMethodShift) 337 } else if v.flag&flagIndir != 0 { 338 fn = *(*unsafe.Pointer)(v.ptr) 339 } else { 340 fn = v.ptr 341 } 342 343 if fn == nil { 344 panic("reflect.Value.Call: call of nil function") 345 } 346 347 isSlice := op == "CallSlice" 348 n := t.NumIn() 349 if isSlice { 350 if !t.IsVariadic() { 351 panic("reflect: CallSlice of non-variadic function") 352 } 353 if len(in) < n { 354 panic("reflect: CallSlice with too few input arguments") 355 } 356 if len(in) > n { 357 panic("reflect: CallSlice with too many input arguments") 358 } 359 } else { 360 if t.IsVariadic() { 361 n-- 362 } 363 if len(in) < n { 364 panic("reflect: Call with too few input arguments") 365 } 366 if !t.IsVariadic() && len(in) > n { 367 panic("reflect: Call with too many input arguments") 368 } 369 } 370 for _, x := range in { 371 if x.Kind() == Invalid { 372 panic("reflect: " + op + " using zero Value argument") 373 } 374 } 375 for i := 0; i < n; i++ { 376 if xt, targ := in[i].Type(), t.In(i); !xt.AssignableTo(targ) { 377 panic("reflect: " + op + " using " + xt.String() + " as type " + targ.String()) 378 } 379 } 380 if !isSlice && t.IsVariadic() { 381 // prepare slice for remaining values 382 m := len(in) - n 383 slice := MakeSlice(t.In(n), m, m) 384 elem := t.In(n).Elem() 385 for i := 0; i < m; i++ { 386 x := in[n+i] 387 if xt := x.Type(); !xt.AssignableTo(elem) { 388 panic("reflect: cannot use " + xt.String() + " as type " + elem.String() + " in " + op) 389 } 390 slice.Index(i).Set(x) 391 } 392 origIn := in 393 in = make([]Value, n+1) 394 copy(in[:n], origIn) 395 in[n] = slice 396 } 397 398 nin := len(in) 399 if nin != t.NumIn() { 400 panic("reflect.Value.Call: wrong argument count") 401 } 402 nout := t.NumOut() 403 404 // Compute frame type. 405 frametype, _, retOffset, _, framePool := funcLayout(t, rcvrtype) 406 407 // Allocate a chunk of memory for frame. 408 var args unsafe.Pointer 409 if nout == 0 { 410 args = framePool.Get().(unsafe.Pointer) 411 } else { 412 // Can't use pool if the function has return values. 413 // We will leak pointer to args in ret, so its lifetime is not scoped. 414 args = unsafe_New(frametype) 415 } 416 off := uintptr(0) 417 418 // Copy inputs into args. 419 if rcvrtype != nil { 420 storeRcvr(rcvr, args) 421 off = ptrSize 422 } 423 for i, v := range in { 424 v.mustBeExported() 425 targ := t.In(i).(*rtype) 426 a := uintptr(targ.align) 427 off = (off + a - 1) &^ (a - 1) 428 n := targ.size 429 if n == 0 { 430 // Not safe to compute args+off pointing at 0 bytes, 431 // because that might point beyond the end of the frame, 432 // but we still need to call assignTo to check assignability. 433 v.assignTo("reflect.Value.Call", targ, nil) 434 continue 435 } 436 addr := add(args, off, "n > 0") 437 v = v.assignTo("reflect.Value.Call", targ, addr) 438 if v.flag&flagIndir != 0 { 439 typedmemmove(targ, addr, v.ptr) 440 } else { 441 *(*unsafe.Pointer)(addr) = v.ptr 442 } 443 off += n 444 } 445 446 // Call. 447 call(frametype, fn, args, uint32(frametype.size), uint32(retOffset)) 448 449 // For testing; see TestCallMethodJump. 450 if callGC { 451 runtime.GC() 452 } 453 454 var ret []Value 455 if nout == 0 { 456 typedmemclr(frametype, args) 457 framePool.Put(args) 458 } else { 459 // Zero the now unused input area of args, 460 // because the Values returned by this function contain pointers to the args object, 461 // and will thus keep the args object alive indefinitely. 462 typedmemclrpartial(frametype, args, 0, retOffset) 463 464 // Wrap Values around return values in args. 465 ret = make([]Value, nout) 466 off = retOffset 467 for i := 0; i < nout; i++ { 468 tv := t.Out(i) 469 a := uintptr(tv.Align()) 470 off = (off + a - 1) &^ (a - 1) 471 if tv.Size() != 0 { 472 fl := flagIndir | flag(tv.Kind()) 473 ret[i] = Value{tv.common(), add(args, off, "tv.Size() != 0"), fl} 474 // Note: this does introduce false sharing between results - 475 // if any result is live, they are all live. 476 // (And the space for the args is live as well, but as we've 477 // cleared that space it isn't as big a deal.) 478 } else { 479 // For zero-sized return value, args+off may point to the next object. 480 // In this case, return the zero value instead. 481 ret[i] = Zero(tv) 482 } 483 off += tv.Size() 484 } 485 } 486 487 return ret 488 } 489 490 // callReflect is the call implementation used by a function 491 // returned by MakeFunc. In many ways it is the opposite of the 492 // method Value.call above. The method above converts a call using Values 493 // into a call of a function with a concrete argument frame, while 494 // callReflect converts a call of a function with a concrete argument 495 // frame into a call using Values. 496 // It is in this file so that it can be next to the call method above. 497 // The remainder of the MakeFunc implementation is in makefunc.go. 498 // 499 // NOTE: This function must be marked as a "wrapper" in the generated code, 500 // so that the linker can make it work correctly for panic and recover. 501 // The gc compilers know to do that for the name "reflect.callReflect". 502 // 503 // ctxt is the "closure" generated by MakeFunc. 504 // frame is a pointer to the arguments to that closure on the stack. 505 // retValid points to a boolean which should be set when the results 506 // section of frame is set. 507 func callReflect(ctxt *makeFuncImpl, frame unsafe.Pointer, retValid *bool) { 508 ftyp := ctxt.ftyp 509 f := ctxt.fn 510 511 // Copy argument frame into Values. 512 ptr := frame 513 off := uintptr(0) 514 in := make([]Value, 0, int(ftyp.inCount)) 515 for _, typ := range ftyp.in() { 516 off += -off & uintptr(typ.align-1) 517 v := Value{typ, nil, flag(typ.Kind())} 518 if ifaceIndir(typ) { 519 // value cannot be inlined in interface data. 520 // Must make a copy, because f might keep a reference to it, 521 // and we cannot let f keep a reference to the stack frame 522 // after this function returns, not even a read-only reference. 523 v.ptr = unsafe_New(typ) 524 if typ.size > 0 { 525 typedmemmove(typ, v.ptr, add(ptr, off, "typ.size > 0")) 526 } 527 v.flag |= flagIndir 528 } else { 529 v.ptr = *(*unsafe.Pointer)(add(ptr, off, "1-ptr")) 530 } 531 in = append(in, v) 532 off += typ.size 533 } 534 535 // Call underlying function. 536 out := f(in) 537 numOut := ftyp.NumOut() 538 if len(out) != numOut { 539 panic("reflect: wrong return count from function created by MakeFunc") 540 } 541 542 // Copy results back into argument frame. 543 if numOut > 0 { 544 off += -off & (ptrSize - 1) 545 if runtime.GOARCH == "amd64p32" { 546 off = align(off, 8) 547 } 548 for i, typ := range ftyp.out() { 549 v := out[i] 550 if v.typ != typ { 551 panic("reflect: function created by MakeFunc using " + funcName(f) + 552 " returned wrong type: have " + 553 out[i].typ.String() + " for " + typ.String()) 554 } 555 if v.flag&flagRO != 0 { 556 panic("reflect: function created by MakeFunc using " + funcName(f) + 557 " returned value obtained from unexported field") 558 } 559 off += -off & uintptr(typ.align-1) 560 if typ.size == 0 { 561 continue 562 } 563 addr := add(ptr, off, "typ.size > 0") 564 // We are writing to stack. No write barrier. 565 if v.flag&flagIndir != 0 { 566 memmove(addr, v.ptr, typ.size) 567 } else { 568 *(*uintptr)(addr) = uintptr(v.ptr) 569 } 570 off += typ.size 571 } 572 } 573 574 // Announce that the return values are valid. 575 // After this point the runtime can depend on the return values being valid. 576 *retValid = true 577 578 // We have to make sure that the out slice lives at least until 579 // the runtime knows the return values are valid. Otherwise, the 580 // return values might not be scanned by anyone during a GC. 581 // (out would be dead, and the return slots not yet alive.) 582 runtime.KeepAlive(out) 583 584 // runtime.getArgInfo expects to be able to find ctxt on the 585 // stack when it finds our caller, makeFuncStub. Make sure it 586 // doesn't get garbage collected. 587 runtime.KeepAlive(ctxt) 588 } 589 590 // methodReceiver returns information about the receiver 591 // described by v. The Value v may or may not have the 592 // flagMethod bit set, so the kind cached in v.flag should 593 // not be used. 594 // The return value rcvrtype gives the method's actual receiver type. 595 // The return value t gives the method type signature (without the receiver). 596 // The return value fn is a pointer to the method code. 597 func methodReceiver(op string, v Value, methodIndex int) (rcvrtype *rtype, t *funcType, fn unsafe.Pointer) { 598 i := methodIndex 599 if v.typ.Kind() == Interface { 600 tt := (*interfaceType)(unsafe.Pointer(v.typ)) 601 if uint(i) >= uint(len(tt.methods)) { 602 panic("reflect: internal error: invalid method index") 603 } 604 m := &tt.methods[i] 605 if !tt.nameOff(m.name).isExported() { 606 panic("reflect: " + op + " of unexported method") 607 } 608 iface := (*nonEmptyInterface)(v.ptr) 609 if iface.itab == nil { 610 panic("reflect: " + op + " of method on nil interface value") 611 } 612 rcvrtype = iface.itab.typ 613 fn = unsafe.Pointer(&iface.itab.fun[i]) 614 t = (*funcType)(unsafe.Pointer(tt.typeOff(m.typ))) 615 } else { 616 rcvrtype = v.typ 617 ms := v.typ.exportedMethods() 618 if uint(i) >= uint(len(ms)) { 619 panic("reflect: internal error: invalid method index") 620 } 621 m := ms[i] 622 if !v.typ.nameOff(m.name).isExported() { 623 panic("reflect: " + op + " of unexported method") 624 } 625 ifn := v.typ.textOff(m.ifn) 626 fn = unsafe.Pointer(&ifn) 627 t = (*funcType)(unsafe.Pointer(v.typ.typeOff(m.mtyp))) 628 } 629 return 630 } 631 632 // v is a method receiver. Store at p the word which is used to 633 // encode that receiver at the start of the argument list. 634 // Reflect uses the "interface" calling convention for 635 // methods, which always uses one word to record the receiver. 636 func storeRcvr(v Value, p unsafe.Pointer) { 637 t := v.typ 638 if t.Kind() == Interface { 639 // the interface data word becomes the receiver word 640 iface := (*nonEmptyInterface)(v.ptr) 641 *(*unsafe.Pointer)(p) = iface.word 642 } else if v.flag&flagIndir != 0 && !ifaceIndir(t) { 643 *(*unsafe.Pointer)(p) = *(*unsafe.Pointer)(v.ptr) 644 } else { 645 *(*unsafe.Pointer)(p) = v.ptr 646 } 647 } 648 649 // align returns the result of rounding x up to a multiple of n. 650 // n must be a power of two. 651 func align(x, n uintptr) uintptr { 652 return (x + n - 1) &^ (n - 1) 653 } 654 655 // callMethod is the call implementation used by a function returned 656 // by makeMethodValue (used by v.Method(i).Interface()). 657 // It is a streamlined version of the usual reflect call: the caller has 658 // already laid out the argument frame for us, so we don't have 659 // to deal with individual Values for each argument. 660 // It is in this file so that it can be next to the two similar functions above. 661 // The remainder of the makeMethodValue implementation is in makefunc.go. 662 // 663 // NOTE: This function must be marked as a "wrapper" in the generated code, 664 // so that the linker can make it work correctly for panic and recover. 665 // The gc compilers know to do that for the name "reflect.callMethod". 666 // 667 // ctxt is the "closure" generated by makeVethodValue. 668 // frame is a pointer to the arguments to that closure on the stack. 669 // retValid points to a boolean which should be set when the results 670 // section of frame is set. 671 func callMethod(ctxt *methodValue, frame unsafe.Pointer, retValid *bool) { 672 rcvr := ctxt.rcvr 673 rcvrtype, t, fn := methodReceiver("call", rcvr, ctxt.method) 674 frametype, argSize, retOffset, _, framePool := funcLayout(t, rcvrtype) 675 676 // Make a new frame that is one word bigger so we can store the receiver. 677 // This space is used for both arguments and return values. 678 scratch := framePool.Get().(unsafe.Pointer) 679 680 // Copy in receiver and rest of args. 681 // Avoid constructing out-of-bounds pointers if there are no args. 682 storeRcvr(rcvr, scratch) 683 if argSize-ptrSize > 0 { 684 typedmemmovepartial(frametype, add(scratch, ptrSize, "argSize > ptrSize"), frame, ptrSize, argSize-ptrSize) 685 } 686 687 // Call. 688 // Call copies the arguments from scratch to the stack, calls fn, 689 // and then copies the results back into scratch. 690 call(frametype, fn, scratch, uint32(frametype.size), uint32(retOffset)) 691 692 // Copy return values. On amd64p32, the beginning of return values 693 // is 64-bit aligned, so the caller's frame layout (which doesn't have 694 // a receiver) is different from the layout of the fn call, which has 695 // a receiver. 696 // Ignore any changes to args and just copy return values. 697 // Avoid constructing out-of-bounds pointers if there are no return values. 698 if frametype.size-retOffset > 0 { 699 callerRetOffset := retOffset - ptrSize 700 if runtime.GOARCH == "amd64p32" { 701 callerRetOffset = align(argSize-ptrSize, 8) 702 } 703 // This copies to the stack. Write barriers are not needed. 704 memmove(add(frame, callerRetOffset, "frametype.size > retOffset"), 705 add(scratch, retOffset, "frametype.size > retOffset"), 706 frametype.size-retOffset) 707 } 708 709 // Tell the runtime it can now depend on the return values 710 // being properly initialized. 711 *retValid = true 712 713 // Clear the scratch space and put it back in the pool. 714 // This must happen after the statement above, so that the return 715 // values will always be scanned by someone. 716 typedmemclr(frametype, scratch) 717 framePool.Put(scratch) 718 719 // See the comment in callReflect. 720 runtime.KeepAlive(ctxt) 721 } 722 723 // funcName returns the name of f, for use in error messages. 724 func funcName(f func([]Value) []Value) string { 725 pc := *(*uintptr)(unsafe.Pointer(&f)) 726 rf := runtime.FuncForPC(pc) 727 if rf != nil { 728 return rf.Name() 729 } 730 return "closure" 731 } 732 733 // Cap returns v's capacity. 734 // It panics if v's Kind is not Array, Chan, or Slice. 735 func (v Value) Cap() int { 736 k := v.kind() 737 switch k { 738 case Array: 739 return v.typ.Len() 740 case Chan: 741 return chancap(v.pointer()) 742 case Slice: 743 // Slice is always bigger than a word; assume flagIndir. 744 return (*sliceHeader)(v.ptr).Cap 745 } 746 panic(&ValueError{"reflect.Value.Cap", v.kind()}) 747 } 748 749 // Close closes the channel v. 750 // It panics if v's Kind is not Chan. 751 func (v Value) Close() { 752 v.mustBe(Chan) 753 v.mustBeExported() 754 chanclose(v.pointer()) 755 } 756 757 // Complex returns v's underlying value, as a complex128. 758 // It panics if v's Kind is not Complex64 or Complex128 759 func (v Value) Complex() complex128 { 760 k := v.kind() 761 switch k { 762 case Complex64: 763 return complex128(*(*complex64)(v.ptr)) 764 case Complex128: 765 return *(*complex128)(v.ptr) 766 } 767 panic(&ValueError{"reflect.Value.Complex", v.kind()}) 768 } 769 770 // Elem returns the value that the interface v contains 771 // or that the pointer v points to. 772 // It panics if v's Kind is not Interface or Ptr. 773 // It returns the zero Value if v is nil. 774 func (v Value) Elem() Value { 775 k := v.kind() 776 switch k { 777 case Interface: 778 var eface interface{} 779 if v.typ.NumMethod() == 0 { 780 eface = *(*interface{})(v.ptr) 781 } else { 782 eface = (interface{})(*(*interface { 783 M() 784 })(v.ptr)) 785 } 786 x := unpackEface(eface) 787 if x.flag != 0 { 788 x.flag |= v.flag.ro() 789 } 790 return x 791 case Ptr: 792 ptr := v.ptr 793 if v.flag&flagIndir != 0 { 794 ptr = *(*unsafe.Pointer)(ptr) 795 } 796 // The returned value's address is v's value. 797 if ptr == nil { 798 return Value{} 799 } 800 tt := (*ptrType)(unsafe.Pointer(v.typ)) 801 typ := tt.elem 802 fl := v.flag&flagRO | flagIndir | flagAddr 803 fl |= flag(typ.Kind()) 804 return Value{typ, ptr, fl} 805 } 806 panic(&ValueError{"reflect.Value.Elem", v.kind()}) 807 } 808 809 // Field returns the i'th field of the struct v. 810 // It panics if v's Kind is not Struct or i is out of range. 811 func (v Value) Field(i int) Value { 812 if v.kind() != Struct { 813 panic(&ValueError{"reflect.Value.Field", v.kind()}) 814 } 815 tt := (*structType)(unsafe.Pointer(v.typ)) 816 if uint(i) >= uint(len(tt.fields)) { 817 panic("reflect: Field index out of range") 818 } 819 field := &tt.fields[i] 820 typ := field.typ 821 822 // Inherit permission bits from v, but clear flagEmbedRO. 823 fl := v.flag&(flagStickyRO|flagIndir|flagAddr) | flag(typ.Kind()) 824 // Using an unexported field forces flagRO. 825 if !field.name.isExported() { 826 if field.embedded() { 827 fl |= flagEmbedRO 828 } else { 829 fl |= flagStickyRO 830 } 831 } 832 // Either flagIndir is set and v.ptr points at struct, 833 // or flagIndir is not set and v.ptr is the actual struct data. 834 // In the former case, we want v.ptr + offset. 835 // In the latter case, we must have field.offset = 0, 836 // so v.ptr + field.offset is still the correct address. 837 ptr := add(v.ptr, field.offset(), "same as non-reflect &v.field") 838 return Value{typ, ptr, fl} 839 } 840 841 // FieldByIndex returns the nested field corresponding to index. 842 // It panics if v's Kind is not struct. 843 func (v Value) FieldByIndex(index []int) Value { 844 if len(index) == 1 { 845 return v.Field(index[0]) 846 } 847 v.mustBe(Struct) 848 for i, x := range index { 849 if i > 0 { 850 if v.Kind() == Ptr && v.typ.Elem().Kind() == Struct { 851 if v.IsNil() { 852 panic("reflect: indirection through nil pointer to embedded struct") 853 } 854 v = v.Elem() 855 } 856 } 857 v = v.Field(x) 858 } 859 return v 860 } 861 862 // FieldByName returns the struct field with the given name. 863 // It returns the zero Value if no field was found. 864 // It panics if v's Kind is not struct. 865 func (v Value) FieldByName(name string) Value { 866 v.mustBe(Struct) 867 if f, ok := v.typ.FieldByName(name); ok { 868 return v.FieldByIndex(f.Index) 869 } 870 return Value{} 871 } 872 873 // FieldByNameFunc returns the struct field with a name 874 // that satisfies the match function. 875 // It panics if v's Kind is not struct. 876 // It returns the zero Value if no field was found. 877 func (v Value) FieldByNameFunc(match func(string) bool) Value { 878 if f, ok := v.typ.FieldByNameFunc(match); ok { 879 return v.FieldByIndex(f.Index) 880 } 881 return Value{} 882 } 883 884 // Float returns v's underlying value, as a float64. 885 // It panics if v's Kind is not Float32 or Float64 886 func (v Value) Float() float64 { 887 k := v.kind() 888 switch k { 889 case Float32: 890 return float64(*(*float32)(v.ptr)) 891 case Float64: 892 return *(*float64)(v.ptr) 893 } 894 panic(&ValueError{"reflect.Value.Float", v.kind()}) 895 } 896 897 var uint8Type = TypeOf(uint8(0)).(*rtype) 898 899 // Index returns v's i'th element. 900 // It panics if v's Kind is not Array, Slice, or String or i is out of range. 901 func (v Value) Index(i int) Value { 902 switch v.kind() { 903 case Array: 904 tt := (*arrayType)(unsafe.Pointer(v.typ)) 905 if uint(i) >= uint(tt.len) { 906 panic("reflect: array index out of range") 907 } 908 typ := tt.elem 909 offset := uintptr(i) * typ.size 910 911 // Either flagIndir is set and v.ptr points at array, 912 // or flagIndir is not set and v.ptr is the actual array data. 913 // In the former case, we want v.ptr + offset. 914 // In the latter case, we must be doing Index(0), so offset = 0, 915 // so v.ptr + offset is still the correct address. 916 val := add(v.ptr, offset, "same as &v[i], i < tt.len") 917 fl := v.flag&(flagIndir|flagAddr) | v.flag.ro() | flag(typ.Kind()) // bits same as overall array 918 return Value{typ, val, fl} 919 920 case Slice: 921 // Element flag same as Elem of Ptr. 922 // Addressable, indirect, possibly read-only. 923 s := (*sliceHeader)(v.ptr) 924 if uint(i) >= uint(s.Len) { 925 panic("reflect: slice index out of range") 926 } 927 tt := (*sliceType)(unsafe.Pointer(v.typ)) 928 typ := tt.elem 929 val := arrayAt(s.Data, i, typ.size, "i < s.Len") 930 fl := flagAddr | flagIndir | v.flag.ro() | flag(typ.Kind()) 931 return Value{typ, val, fl} 932 933 case String: 934 s := (*stringHeader)(v.ptr) 935 if uint(i) >= uint(s.Len) { 936 panic("reflect: string index out of range") 937 } 938 p := arrayAt(s.Data, i, 1, "i < s.Len") 939 fl := v.flag.ro() | flag(Uint8) | flagIndir 940 return Value{uint8Type, p, fl} 941 } 942 panic(&ValueError{"reflect.Value.Index", v.kind()}) 943 } 944 945 // Int returns v's underlying value, as an int64. 946 // It panics if v's Kind is not Int, Int8, Int16, Int32, or Int64. 947 func (v Value) Int() int64 { 948 k := v.kind() 949 p := v.ptr 950 switch k { 951 case Int: 952 return int64(*(*int)(p)) 953 case Int8: 954 return int64(*(*int8)(p)) 955 case Int16: 956 return int64(*(*int16)(p)) 957 case Int32: 958 return int64(*(*int32)(p)) 959 case Int64: 960 return *(*int64)(p) 961 } 962 panic(&ValueError{"reflect.Value.Int", v.kind()}) 963 } 964 965 // CanInterface reports whether Interface can be used without panicking. 966 func (v Value) CanInterface() bool { 967 if v.flag == 0 { 968 panic(&ValueError{"reflect.Value.CanInterface", Invalid}) 969 } 970 return v.flag&flagRO == 0 971 } 972 973 // Interface returns v's current value as an interface{}. 974 // It is equivalent to: 975 // var i interface{} = (v's underlying value) 976 // It panics if the Value was obtained by accessing 977 // unexported struct fields. 978 func (v Value) Interface() (i interface{}) { 979 return valueInterface(v, true) 980 } 981 982 func valueInterface(v Value, safe bool) interface{} { 983 if v.flag == 0 { 984 panic(&ValueError{"reflect.Value.Interface", 0}) 985 } 986 if safe && v.flag&flagRO != 0 { 987 // Do not allow access to unexported values via Interface, 988 // because they might be pointers that should not be 989 // writable or methods or function that should not be callable. 990 panic("reflect.Value.Interface: cannot return value obtained from unexported field or method") 991 } 992 if v.flag&flagMethod != 0 { 993 v = makeMethodValue("Interface", v) 994 } 995 996 if v.kind() == Interface { 997 // Special case: return the element inside the interface. 998 // Empty interface has one layout, all interfaces with 999 // methods have a second layout. 1000 if v.NumMethod() == 0 { 1001 return *(*interface{})(v.ptr) 1002 } 1003 return *(*interface { 1004 M() 1005 })(v.ptr) 1006 } 1007 1008 // TODO: pass safe to packEface so we don't need to copy if safe==true? 1009 return packEface(v) 1010 } 1011 1012 // InterfaceData returns the interface v's value as a uintptr pair. 1013 // It panics if v's Kind is not Interface. 1014 func (v Value) InterfaceData() [2]uintptr { 1015 // TODO: deprecate this 1016 v.mustBe(Interface) 1017 // We treat this as a read operation, so we allow 1018 // it even for unexported data, because the caller 1019 // has to import "unsafe" to turn it into something 1020 // that can be abused. 1021 // Interface value is always bigger than a word; assume flagIndir. 1022 return *(*[2]uintptr)(v.ptr) 1023 } 1024 1025 // IsNil reports whether its argument v is nil. The argument must be 1026 // a chan, func, interface, map, pointer, or slice value; if it is 1027 // not, IsNil panics. Note that IsNil is not always equivalent to a 1028 // regular comparison with nil in Go. For example, if v was created 1029 // by calling ValueOf with an uninitialized interface variable i, 1030 // i==nil will be true but v.IsNil will panic as v will be the zero 1031 // Value. 1032 func (v Value) IsNil() bool { 1033 k := v.kind() 1034 switch k { 1035 case Chan, Func, Map, Ptr, UnsafePointer: 1036 if v.flag&flagMethod != 0 { 1037 return false 1038 } 1039 ptr := v.ptr 1040 if v.flag&flagIndir != 0 { 1041 ptr = *(*unsafe.Pointer)(ptr) 1042 } 1043 return ptr == nil 1044 case Interface, Slice: 1045 // Both interface and slice are nil if first word is 0. 1046 // Both are always bigger than a word; assume flagIndir. 1047 return *(*unsafe.Pointer)(v.ptr) == nil 1048 } 1049 panic(&ValueError{"reflect.Value.IsNil", v.kind()}) 1050 } 1051 1052 // IsValid reports whether v represents a value. 1053 // It returns false if v is the zero Value. 1054 // If IsValid returns false, all other methods except String panic. 1055 // Most functions and methods never return an invalid value. 1056 // If one does, its documentation states the conditions explicitly. 1057 func (v Value) IsValid() bool { 1058 return v.flag != 0 1059 } 1060 1061 // Kind returns v's Kind. 1062 // If v is the zero Value (IsValid returns false), Kind returns Invalid. 1063 func (v Value) Kind() Kind { 1064 return v.kind() 1065 } 1066 1067 // Len returns v's length. 1068 // It panics if v's Kind is not Array, Chan, Map, Slice, or String. 1069 func (v Value) Len() int { 1070 k := v.kind() 1071 switch k { 1072 case Array: 1073 tt := (*arrayType)(unsafe.Pointer(v.typ)) 1074 return int(tt.len) 1075 case Chan: 1076 return chanlen(v.pointer()) 1077 case Map: 1078 return maplen(v.pointer()) 1079 case Slice: 1080 // Slice is bigger than a word; assume flagIndir. 1081 return (*sliceHeader)(v.ptr).Len 1082 case String: 1083 // String is bigger than a word; assume flagIndir. 1084 return (*stringHeader)(v.ptr).Len 1085 } 1086 panic(&ValueError{"reflect.Value.Len", v.kind()}) 1087 } 1088 1089 // MapIndex returns the value associated with key in the map v. 1090 // It panics if v's Kind is not Map. 1091 // It returns the zero Value if key is not found in the map or if v represents a nil map. 1092 // As in Go, the key's value must be assignable to the map's key type. 1093 func (v Value) MapIndex(key Value) Value { 1094 v.mustBe(Map) 1095 tt := (*mapType)(unsafe.Pointer(v.typ)) 1096 1097 // Do not require key to be exported, so that DeepEqual 1098 // and other programs can use all the keys returned by 1099 // MapKeys as arguments to MapIndex. If either the map 1100 // or the key is unexported, though, the result will be 1101 // considered unexported. This is consistent with the 1102 // behavior for structs, which allow read but not write 1103 // of unexported fields. 1104 key = key.assignTo("reflect.Value.MapIndex", tt.key, nil) 1105 1106 var k unsafe.Pointer 1107 if key.flag&flagIndir != 0 { 1108 k = key.ptr 1109 } else { 1110 k = unsafe.Pointer(&key.ptr) 1111 } 1112 e := mapaccess(v.typ, v.pointer(), k) 1113 if e == nil { 1114 return Value{} 1115 } 1116 typ := tt.elem 1117 fl := (v.flag | key.flag).ro() 1118 fl |= flag(typ.Kind()) 1119 return copyVal(typ, fl, e) 1120 } 1121 1122 // MapKeys returns a slice containing all the keys present in the map, 1123 // in unspecified order. 1124 // It panics if v's Kind is not Map. 1125 // It returns an empty slice if v represents a nil map. 1126 func (v Value) MapKeys() []Value { 1127 v.mustBe(Map) 1128 tt := (*mapType)(unsafe.Pointer(v.typ)) 1129 keyType := tt.key 1130 1131 fl := v.flag.ro() | flag(keyType.Kind()) 1132 1133 m := v.pointer() 1134 mlen := int(0) 1135 if m != nil { 1136 mlen = maplen(m) 1137 } 1138 it := mapiterinit(v.typ, m) 1139 a := make([]Value, mlen) 1140 var i int 1141 for i = 0; i < len(a); i++ { 1142 key := mapiterkey(it) 1143 if key == nil { 1144 // Someone deleted an entry from the map since we 1145 // called maplen above. It's a data race, but nothing 1146 // we can do about it. 1147 break 1148 } 1149 a[i] = copyVal(keyType, fl, key) 1150 mapiternext(it) 1151 } 1152 return a[:i] 1153 } 1154 1155 // A MapIter is an iterator for ranging over a map. 1156 // See Value.MapRange. 1157 type MapIter struct { 1158 m Value 1159 it unsafe.Pointer 1160 } 1161 1162 // Key returns the key of the iterator's current map entry. 1163 func (it *MapIter) Key() Value { 1164 if it.it == nil { 1165 panic("MapIter.Key called before Next") 1166 } 1167 if mapiterkey(it.it) == nil { 1168 panic("MapIter.Key called on exhausted iterator") 1169 } 1170 1171 t := (*mapType)(unsafe.Pointer(it.m.typ)) 1172 ktype := t.key 1173 return copyVal(ktype, it.m.flag.ro()|flag(ktype.Kind()), mapiterkey(it.it)) 1174 } 1175 1176 // Value returns the value of the iterator's current map entry. 1177 func (it *MapIter) Value() Value { 1178 if it.it == nil { 1179 panic("MapIter.Value called before Next") 1180 } 1181 if mapiterkey(it.it) == nil { 1182 panic("MapIter.Value called on exhausted iterator") 1183 } 1184 1185 t := (*mapType)(unsafe.Pointer(it.m.typ)) 1186 vtype := t.elem 1187 return copyVal(vtype, it.m.flag.ro()|flag(vtype.Kind()), mapitervalue(it.it)) 1188 } 1189 1190 // Next advances the map iterator and reports whether there is another 1191 // entry. It returns false when the iterator is exhausted; subsequent 1192 // calls to Key, Value, or Next will panic. 1193 func (it *MapIter) Next() bool { 1194 if it.it == nil { 1195 it.it = mapiterinit(it.m.typ, it.m.pointer()) 1196 } else { 1197 if mapiterkey(it.it) == nil { 1198 panic("MapIter.Next called on exhausted iterator") 1199 } 1200 mapiternext(it.it) 1201 } 1202 return mapiterkey(it.it) != nil 1203 } 1204 1205 // MapRange returns a range iterator for a map. 1206 // It panics if v's Kind is not Map. 1207 // 1208 // Call Next to advance the iterator, and Key/Value to access each entry. 1209 // Next returns false when the iterator is exhausted. 1210 // MapRange follows the same iteration semantics as a range statement. 1211 // 1212 // Example: 1213 // 1214 // iter := reflect.ValueOf(m).MapRange() 1215 // for iter.Next() { 1216 // k := iter.Key() 1217 // v := iter.Value() 1218 // ... 1219 // } 1220 // 1221 func (v Value) MapRange() *MapIter { 1222 v.mustBe(Map) 1223 return &MapIter{m: v} 1224 } 1225 1226 // copyVal returns a Value containing the map key or value at ptr, 1227 // allocating a new variable as needed. 1228 func copyVal(typ *rtype, fl flag, ptr unsafe.Pointer) Value { 1229 if ifaceIndir(typ) { 1230 // Copy result so future changes to the map 1231 // won't change the underlying value. 1232 c := unsafe_New(typ) 1233 typedmemmove(typ, c, ptr) 1234 return Value{typ, c, fl | flagIndir} 1235 } 1236 return Value{typ, *(*unsafe.Pointer)(ptr), fl} 1237 } 1238 1239 // Method returns a function value corresponding to v's i'th method. 1240 // The arguments to a Call on the returned function should not include 1241 // a receiver; the returned function will always use v as the receiver. 1242 // Method panics if i is out of range or if v is a nil interface value. 1243 func (v Value) Method(i int) Value { 1244 if v.typ == nil { 1245 panic(&ValueError{"reflect.Value.Method", Invalid}) 1246 } 1247 if v.flag&flagMethod != 0 || uint(i) >= uint(v.typ.NumMethod()) { 1248 panic("reflect: Method index out of range") 1249 } 1250 if v.typ.Kind() == Interface && v.IsNil() { 1251 panic("reflect: Method on nil interface value") 1252 } 1253 fl := v.flag & (flagStickyRO | flagIndir) // Clear flagEmbedRO 1254 fl |= flag(Func) 1255 fl |= flag(i)<<flagMethodShift | flagMethod 1256 return Value{v.typ, v.ptr, fl} 1257 } 1258 1259 // NumMethod returns the number of exported methods in the value's method set. 1260 func (v Value) NumMethod() int { 1261 if v.typ == nil { 1262 panic(&ValueError{"reflect.Value.NumMethod", Invalid}) 1263 } 1264 if v.flag&flagMethod != 0 { 1265 return 0 1266 } 1267 return v.typ.NumMethod() 1268 } 1269 1270 // MethodByName returns a function value corresponding to the method 1271 // of v with the given name. 1272 // The arguments to a Call on the returned function should not include 1273 // a receiver; the returned function will always use v as the receiver. 1274 // It returns the zero Value if no method was found. 1275 func (v Value) MethodByName(name string) Value { 1276 if v.typ == nil { 1277 panic(&ValueError{"reflect.Value.MethodByName", Invalid}) 1278 } 1279 if v.flag&flagMethod != 0 { 1280 return Value{} 1281 } 1282 m, ok := v.typ.MethodByName(name) 1283 if !ok { 1284 return Value{} 1285 } 1286 return v.Method(m.Index) 1287 } 1288 1289 // NumField returns the number of fields in the struct v. 1290 // It panics if v's Kind is not Struct. 1291 func (v Value) NumField() int { 1292 v.mustBe(Struct) 1293 tt := (*structType)(unsafe.Pointer(v.typ)) 1294 return len(tt.fields) 1295 } 1296 1297 // OverflowComplex reports whether the complex128 x cannot be represented by v's type. 1298 // It panics if v's Kind is not Complex64 or Complex128. 1299 func (v Value) OverflowComplex(x complex128) bool { 1300 k := v.kind() 1301 switch k { 1302 case Complex64: 1303 return overflowFloat32(real(x)) || overflowFloat32(imag(x)) 1304 case Complex128: 1305 return false 1306 } 1307 panic(&ValueError{"reflect.Value.OverflowComplex", v.kind()}) 1308 } 1309 1310 // OverflowFloat reports whether the float64 x cannot be represented by v's type. 1311 // It panics if v's Kind is not Float32 or Float64. 1312 func (v Value) OverflowFloat(x float64) bool { 1313 k := v.kind() 1314 switch k { 1315 case Float32: 1316 return overflowFloat32(x) 1317 case Float64: 1318 return false 1319 } 1320 panic(&ValueError{"reflect.Value.OverflowFloat", v.kind()}) 1321 } 1322 1323 func overflowFloat32(x float64) bool { 1324 if x < 0 { 1325 x = -x 1326 } 1327 return math.MaxFloat32 < x && x <= math.MaxFloat64 1328 } 1329 1330 // OverflowInt reports whether the int64 x cannot be represented by v's type. 1331 // It panics if v's Kind is not Int, Int8, Int16, Int32, or Int64. 1332 func (v Value) OverflowInt(x int64) bool { 1333 k := v.kind() 1334 switch k { 1335 case Int, Int8, Int16, Int32, Int64: 1336 bitSize := v.typ.size * 8 1337 trunc := (x << (64 - bitSize)) >> (64 - bitSize) 1338 return x != trunc 1339 } 1340 panic(&ValueError{"reflect.Value.OverflowInt", v.kind()}) 1341 } 1342 1343 // OverflowUint reports whether the uint64 x cannot be represented by v's type. 1344 // It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64. 1345 func (v Value) OverflowUint(x uint64) bool { 1346 k := v.kind() 1347 switch k { 1348 case Uint, Uintptr, Uint8, Uint16, Uint32, Uint64: 1349 bitSize := v.typ.size * 8 1350 trunc := (x << (64 - bitSize)) >> (64 - bitSize) 1351 return x != trunc 1352 } 1353 panic(&ValueError{"reflect.Value.OverflowUint", v.kind()}) 1354 } 1355 1356 // Pointer returns v's value as a uintptr. 1357 // It returns uintptr instead of unsafe.Pointer so that 1358 // code using reflect cannot obtain unsafe.Pointers 1359 // without importing the unsafe package explicitly. 1360 // It panics if v's Kind is not Chan, Func, Map, Ptr, Slice, or UnsafePointer. 1361 // 1362 // If v's Kind is Func, the returned pointer is an underlying 1363 // code pointer, but not necessarily enough to identify a 1364 // single function uniquely. The only guarantee is that the 1365 // result is zero if and only if v is a nil func Value. 1366 // 1367 // If v's Kind is Slice, the returned pointer is to the first 1368 // element of the slice. If the slice is nil the returned value 1369 // is 0. If the slice is empty but non-nil the return value is non-zero. 1370 func (v Value) Pointer() uintptr { 1371 // TODO: deprecate 1372 k := v.kind() 1373 switch k { 1374 case Chan, Map, Ptr, UnsafePointer: 1375 return uintptr(v.pointer()) 1376 case Func: 1377 if v.flag&flagMethod != 0 { 1378 // As the doc comment says, the returned pointer is an 1379 // underlying code pointer but not necessarily enough to 1380 // identify a single function uniquely. All method expressions 1381 // created via reflect have the same underlying code pointer, 1382 // so their Pointers are equal. The function used here must 1383 // match the one used in makeMethodValue. 1384 f := methodValueCall 1385 return **(**uintptr)(unsafe.Pointer(&f)) 1386 } 1387 p := v.pointer() 1388 // Non-nil func value points at data block. 1389 // First word of data block is actual code. 1390 if p != nil { 1391 p = *(*unsafe.Pointer)(p) 1392 } 1393 return uintptr(p) 1394 1395 case Slice: 1396 return (*SliceHeader)(v.ptr).Data 1397 } 1398 panic(&ValueError{"reflect.Value.Pointer", v.kind()}) 1399 } 1400 1401 // Recv receives and returns a value from the channel v. 1402 // It panics if v's Kind is not Chan. 1403 // The receive blocks until a value is ready. 1404 // The boolean value ok is true if the value x corresponds to a send 1405 // on the channel, false if it is a zero value received because the channel is closed. 1406 func (v Value) Recv() (x Value, ok bool) { 1407 v.mustBe(Chan) 1408 v.mustBeExported() 1409 return v.recv(false) 1410 } 1411 1412 // internal recv, possibly non-blocking (nb). 1413 // v is known to be a channel. 1414 func (v Value) recv(nb bool) (val Value, ok bool) { 1415 tt := (*chanType)(unsafe.Pointer(v.typ)) 1416 if ChanDir(tt.dir)&RecvDir == 0 { 1417 panic("reflect: recv on send-only channel") 1418 } 1419 t := tt.elem 1420 val = Value{t, nil, flag(t.Kind())} 1421 var p unsafe.Pointer 1422 if ifaceIndir(t) { 1423 p = unsafe_New(t) 1424 val.ptr = p 1425 val.flag |= flagIndir 1426 } else { 1427 p = unsafe.Pointer(&val.ptr) 1428 } 1429 selected, ok := chanrecv(v.pointer(), nb, p) 1430 if !selected { 1431 val = Value{} 1432 } 1433 return 1434 } 1435 1436 // Send sends x on the channel v. 1437 // It panics if v's kind is not Chan or if x's type is not the same type as v's element type. 1438 // As in Go, x's value must be assignable to the channel's element type. 1439 func (v Value) Send(x Value) { 1440 v.mustBe(Chan) 1441 v.mustBeExported() 1442 v.send(x, false) 1443 } 1444 1445 // internal send, possibly non-blocking. 1446 // v is known to be a channel. 1447 func (v Value) send(x Value, nb bool) (selected bool) { 1448 tt := (*chanType)(unsafe.Pointer(v.typ)) 1449 if ChanDir(tt.dir)&SendDir == 0 { 1450 panic("reflect: send on recv-only channel") 1451 } 1452 x.mustBeExported() 1453 x = x.assignTo("reflect.Value.Send", tt.elem, nil) 1454 var p unsafe.Pointer 1455 if x.flag&flagIndir != 0 { 1456 p = x.ptr 1457 } else { 1458 p = unsafe.Pointer(&x.ptr) 1459 } 1460 return chansend(v.pointer(), p, nb) 1461 } 1462 1463 // Set assigns x to the value v. 1464 // It panics if CanSet returns false. 1465 // As in Go, x's value must be assignable to v's type. 1466 func (v Value) Set(x Value) { 1467 v.mustBeAssignable() 1468 x.mustBeExported() // do not let unexported x leak 1469 var target unsafe.Pointer 1470 if v.kind() == Interface { 1471 target = v.ptr 1472 } 1473 x = x.assignTo("reflect.Set", v.typ, target) 1474 if x.flag&flagIndir != 0 { 1475 typedmemmove(v.typ, v.ptr, x.ptr) 1476 } else { 1477 *(*unsafe.Pointer)(v.ptr) = x.ptr 1478 } 1479 } 1480 1481 // SetBool sets v's underlying value. 1482 // It panics if v's Kind is not Bool or if CanSet() is false. 1483 func (v Value) SetBool(x bool) { 1484 v.mustBeAssignable() 1485 v.mustBe(Bool) 1486 *(*bool)(v.ptr) = x 1487 } 1488 1489 // SetBytes sets v's underlying value. 1490 // It panics if v's underlying value is not a slice of bytes. 1491 func (v Value) SetBytes(x []byte) { 1492 v.mustBeAssignable() 1493 v.mustBe(Slice) 1494 if v.typ.Elem().Kind() != Uint8 { 1495 panic("reflect.Value.SetBytes of non-byte slice") 1496 } 1497 *(*[]byte)(v.ptr) = x 1498 } 1499 1500 // setRunes sets v's underlying value. 1501 // It panics if v's underlying value is not a slice of runes (int32s). 1502 func (v Value) setRunes(x []rune) { 1503 v.mustBeAssignable() 1504 v.mustBe(Slice) 1505 if v.typ.Elem().Kind() != Int32 { 1506 panic("reflect.Value.setRunes of non-rune slice") 1507 } 1508 *(*[]rune)(v.ptr) = x 1509 } 1510 1511 // SetComplex sets v's underlying value to x. 1512 // It panics if v's Kind is not Complex64 or Complex128, or if CanSet() is false. 1513 func (v Value) SetComplex(x complex128) { 1514 v.mustBeAssignable() 1515 switch k := v.kind(); k { 1516 default: 1517 panic(&ValueError{"reflect.Value.SetComplex", v.kind()}) 1518 case Complex64: 1519 *(*complex64)(v.ptr) = complex64(x) 1520 case Complex128: 1521 *(*complex128)(v.ptr) = x 1522 } 1523 } 1524 1525 // SetFloat sets v's underlying value to x. 1526 // It panics if v's Kind is not Float32 or Float64, or if CanSet() is false. 1527 func (v Value) SetFloat(x float64) { 1528 v.mustBeAssignable() 1529 switch k := v.kind(); k { 1530 default: 1531 panic(&ValueError{"reflect.Value.SetFloat", v.kind()}) 1532 case Float32: 1533 *(*float32)(v.ptr) = float32(x) 1534 case Float64: 1535 *(*float64)(v.ptr) = x 1536 } 1537 } 1538 1539 // SetInt sets v's underlying value to x. 1540 // It panics if v's Kind is not Int, Int8, Int16, Int32, or Int64, or if CanSet() is false. 1541 func (v Value) SetInt(x int64) { 1542 v.mustBeAssignable() 1543 switch k := v.kind(); k { 1544 default: 1545 panic(&ValueError{"reflect.Value.SetInt", v.kind()}) 1546 case Int: 1547 *(*int)(v.ptr) = int(x) 1548 case Int8: 1549 *(*int8)(v.ptr) = int8(x) 1550 case Int16: 1551 *(*int16)(v.ptr) = int16(x) 1552 case Int32: 1553 *(*int32)(v.ptr) = int32(x) 1554 case Int64: 1555 *(*int64)(v.ptr) = x 1556 } 1557 } 1558 1559 // SetLen sets v's length to n. 1560 // It panics if v's Kind is not Slice or if n is negative or 1561 // greater than the capacity of the slice. 1562 func (v Value) SetLen(n int) { 1563 v.mustBeAssignable() 1564 v.mustBe(Slice) 1565 s := (*sliceHeader)(v.ptr) 1566 if uint(n) > uint(s.Cap) { 1567 panic("reflect: slice length out of range in SetLen") 1568 } 1569 s.Len = n 1570 } 1571 1572 // SetCap sets v's capacity to n. 1573 // It panics if v's Kind is not Slice or if n is smaller than the length or 1574 // greater than the capacity of the slice. 1575 func (v Value) SetCap(n int) { 1576 v.mustBeAssignable() 1577 v.mustBe(Slice) 1578 s := (*sliceHeader)(v.ptr) 1579 if n < s.Len || n > s.Cap { 1580 panic("reflect: slice capacity out of range in SetCap") 1581 } 1582 s.Cap = n 1583 } 1584 1585 // SetMapIndex sets the value associated with key in the map v to val. 1586 // It panics if v's Kind is not Map. 1587 // If val is the zero Value, SetMapIndex deletes the key from the map. 1588 // Otherwise if v holds a nil map, SetMapIndex will panic. 1589 // As in Go, key's value must be assignable to the map's key type, 1590 // and val's value must be assignable to the map's value type. 1591 func (v Value) SetMapIndex(key, val Value) { 1592 v.mustBe(Map) 1593 v.mustBeExported() 1594 key.mustBeExported() 1595 tt := (*mapType)(unsafe.Pointer(v.typ)) 1596 key = key.assignTo("reflect.Value.SetMapIndex", tt.key, nil) 1597 var k unsafe.Pointer 1598 if key.flag&flagIndir != 0 { 1599 k = key.ptr 1600 } else { 1601 k = unsafe.Pointer(&key.ptr) 1602 } 1603 if val.typ == nil { 1604 mapdelete(v.typ, v.pointer(), k) 1605 return 1606 } 1607 val.mustBeExported() 1608 val = val.assignTo("reflect.Value.SetMapIndex", tt.elem, nil) 1609 var e unsafe.Pointer 1610 if val.flag&flagIndir != 0 { 1611 e = val.ptr 1612 } else { 1613 e = unsafe.Pointer(&val.ptr) 1614 } 1615 mapassign(v.typ, v.pointer(), k, e) 1616 } 1617 1618 // SetUint sets v's underlying value to x. 1619 // It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64, or if CanSet() is false. 1620 func (v Value) SetUint(x uint64) { 1621 v.mustBeAssignable() 1622 switch k := v.kind(); k { 1623 default: 1624 panic(&ValueError{"reflect.Value.SetUint", v.kind()}) 1625 case Uint: 1626 *(*uint)(v.ptr) = uint(x) 1627 case Uint8: 1628 *(*uint8)(v.ptr) = uint8(x) 1629 case Uint16: 1630 *(*uint16)(v.ptr) = uint16(x) 1631 case Uint32: 1632 *(*uint32)(v.ptr) = uint32(x) 1633 case Uint64: 1634 *(*uint64)(v.ptr) = x 1635 case Uintptr: 1636 *(*uintptr)(v.ptr) = uintptr(x) 1637 } 1638 } 1639 1640 // SetPointer sets the unsafe.Pointer value v to x. 1641 // It panics if v's Kind is not UnsafePointer. 1642 func (v Value) SetPointer(x unsafe.Pointer) { 1643 v.mustBeAssignable() 1644 v.mustBe(UnsafePointer) 1645 *(*unsafe.Pointer)(v.ptr) = x 1646 } 1647 1648 // SetString sets v's underlying value to x. 1649 // It panics if v's Kind is not String or if CanSet() is false. 1650 func (v Value) SetString(x string) { 1651 v.mustBeAssignable() 1652 v.mustBe(String) 1653 *(*string)(v.ptr) = x 1654 } 1655 1656 // Slice returns v[i:j]. 1657 // It panics if v's Kind is not Array, Slice or String, or if v is an unaddressable array, 1658 // or if the indexes are out of bounds. 1659 func (v Value) Slice(i, j int) Value { 1660 var ( 1661 cap int 1662 typ *sliceType 1663 base unsafe.Pointer 1664 ) 1665 switch kind := v.kind(); kind { 1666 default: 1667 panic(&ValueError{"reflect.Value.Slice", v.kind()}) 1668 1669 case Array: 1670 if v.flag&flagAddr == 0 { 1671 panic("reflect.Value.Slice: slice of unaddressable array") 1672 } 1673 tt := (*arrayType)(unsafe.Pointer(v.typ)) 1674 cap = int(tt.len) 1675 typ = (*sliceType)(unsafe.Pointer(tt.slice)) 1676 base = v.ptr 1677 1678 case Slice: 1679 typ = (*sliceType)(unsafe.Pointer(v.typ)) 1680 s := (*sliceHeader)(v.ptr) 1681 base = s.Data 1682 cap = s.Cap 1683 1684 case String: 1685 s := (*stringHeader)(v.ptr) 1686 if i < 0 || j < i || j > s.Len { 1687 panic("reflect.Value.Slice: string slice index out of bounds") 1688 } 1689 var t stringHeader 1690 if i < s.Len { 1691 t = stringHeader{arrayAt(s.Data, i, 1, "i < s.Len"), j - i} 1692 } 1693 return Value{v.typ, unsafe.Pointer(&t), v.flag} 1694 } 1695 1696 if i < 0 || j < i || j > cap { 1697 panic("reflect.Value.Slice: slice index out of bounds") 1698 } 1699 1700 // Declare slice so that gc can see the base pointer in it. 1701 var x []unsafe.Pointer 1702 1703 // Reinterpret as *sliceHeader to edit. 1704 s := (*sliceHeader)(unsafe.Pointer(&x)) 1705 s.Len = j - i 1706 s.Cap = cap - i 1707 if cap-i > 0 { 1708 s.Data = arrayAt(base, i, typ.elem.Size(), "i < cap") 1709 } else { 1710 // do not advance pointer, to avoid pointing beyond end of slice 1711 s.Data = base 1712 } 1713 1714 fl := v.flag.ro() | flagIndir | flag(Slice) 1715 return Value{typ.common(), unsafe.Pointer(&x), fl} 1716 } 1717 1718 // Slice3 is the 3-index form of the slice operation: it returns v[i:j:k]. 1719 // It panics if v's Kind is not Array or Slice, or if v is an unaddressable array, 1720 // or if the indexes are out of bounds. 1721 func (v Value) Slice3(i, j, k int) Value { 1722 var ( 1723 cap int 1724 typ *sliceType 1725 base unsafe.Pointer 1726 ) 1727 switch kind := v.kind(); kind { 1728 default: 1729 panic(&ValueError{"reflect.Value.Slice3", v.kind()}) 1730 1731 case Array: 1732 if v.flag&flagAddr == 0 { 1733 panic("reflect.Value.Slice3: slice of unaddressable array") 1734 } 1735 tt := (*arrayType)(unsafe.Pointer(v.typ)) 1736 cap = int(tt.len) 1737 typ = (*sliceType)(unsafe.Pointer(tt.slice)) 1738 base = v.ptr 1739 1740 case Slice: 1741 typ = (*sliceType)(unsafe.Pointer(v.typ)) 1742 s := (*sliceHeader)(v.ptr) 1743 base = s.Data 1744 cap = s.Cap 1745 } 1746 1747 if i < 0 || j < i || k < j || k > cap { 1748 panic("reflect.Value.Slice3: slice index out of bounds") 1749 } 1750 1751 // Declare slice so that the garbage collector 1752 // can see the base pointer in it. 1753 var x []unsafe.Pointer 1754 1755 // Reinterpret as *sliceHeader to edit. 1756 s := (*sliceHeader)(unsafe.Pointer(&x)) 1757 s.Len = j - i 1758 s.Cap = k - i 1759 if k-i > 0 { 1760 s.Data = arrayAt(base, i, typ.elem.Size(), "i < k <= cap") 1761 } else { 1762 // do not advance pointer, to avoid pointing beyond end of slice 1763 s.Data = base 1764 } 1765 1766 fl := v.flag.ro() | flagIndir | flag(Slice) 1767 return Value{typ.common(), unsafe.Pointer(&x), fl} 1768 } 1769 1770 // String returns the string v's underlying value, as a string. 1771 // String is a special case because of Go's String method convention. 1772 // Unlike the other getters, it does not panic if v's Kind is not String. 1773 // Instead, it returns a string of the form "<T value>" where T is v's type. 1774 // The fmt package treats Values specially. It does not call their String 1775 // method implicitly but instead prints the concrete values they hold. 1776 func (v Value) String() string { 1777 switch k := v.kind(); k { 1778 case Invalid: 1779 return "<invalid Value>" 1780 case String: 1781 return *(*string)(v.ptr) 1782 } 1783 // If you call String on a reflect.Value of other type, it's better to 1784 // print something than to panic. Useful in debugging. 1785 return "<" + v.Type().String() + " Value>" 1786 } 1787 1788 // TryRecv attempts to receive a value from the channel v but will not block. 1789 // It panics if v's Kind is not Chan. 1790 // If the receive delivers a value, x is the transferred value and ok is true. 1791 // If the receive cannot finish without blocking, x is the zero Value and ok is false. 1792 // If the channel is closed, x is the zero value for the channel's element type and ok is false. 1793 func (v Value) TryRecv() (x Value, ok bool) { 1794 v.mustBe(Chan) 1795 v.mustBeExported() 1796 return v.recv(true) 1797 } 1798 1799 // TrySend attempts to send x on the channel v but will not block. 1800 // It panics if v's Kind is not Chan. 1801 // It reports whether the value was sent. 1802 // As in Go, x's value must be assignable to the channel's element type. 1803 func (v Value) TrySend(x Value) bool { 1804 v.mustBe(Chan) 1805 v.mustBeExported() 1806 return v.send(x, true) 1807 } 1808 1809 // Type returns v's type. 1810 func (v Value) Type() Type { 1811 f := v.flag 1812 if f == 0 { 1813 panic(&ValueError{"reflect.Value.Type", Invalid}) 1814 } 1815 if f&flagMethod == 0 { 1816 // Easy case 1817 return v.typ 1818 } 1819 1820 // Method value. 1821 // v.typ describes the receiver, not the method type. 1822 i := int(v.flag) >> flagMethodShift 1823 if v.typ.Kind() == Interface { 1824 // Method on interface. 1825 tt := (*interfaceType)(unsafe.Pointer(v.typ)) 1826 if uint(i) >= uint(len(tt.methods)) { 1827 panic("reflect: internal error: invalid method index") 1828 } 1829 m := &tt.methods[i] 1830 return v.typ.typeOff(m.typ) 1831 } 1832 // Method on concrete type. 1833 ms := v.typ.exportedMethods() 1834 if uint(i) >= uint(len(ms)) { 1835 panic("reflect: internal error: invalid method index") 1836 } 1837 m := ms[i] 1838 return v.typ.typeOff(m.mtyp) 1839 } 1840 1841 // Uint returns v's underlying value, as a uint64. 1842 // It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64. 1843 func (v Value) Uint() uint64 { 1844 k := v.kind() 1845 p := v.ptr 1846 switch k { 1847 case Uint: 1848 return uint64(*(*uint)(p)) 1849 case Uint8: 1850 return uint64(*(*uint8)(p)) 1851 case Uint16: 1852 return uint64(*(*uint16)(p)) 1853 case Uint32: 1854 return uint64(*(*uint32)(p)) 1855 case Uint64: 1856 return *(*uint64)(p) 1857 case Uintptr: 1858 return uint64(*(*uintptr)(p)) 1859 } 1860 panic(&ValueError{"reflect.Value.Uint", v.kind()}) 1861 } 1862 1863 // UnsafeAddr returns a pointer to v's data. 1864 // It is for advanced clients that also import the "unsafe" package. 1865 // It panics if v is not addressable. 1866 func (v Value) UnsafeAddr() uintptr { 1867 // TODO: deprecate 1868 if v.typ == nil { 1869 panic(&ValueError{"reflect.Value.UnsafeAddr", Invalid}) 1870 } 1871 if v.flag&flagAddr == 0 { 1872 panic("reflect.Value.UnsafeAddr of unaddressable value") 1873 } 1874 return uintptr(v.ptr) 1875 } 1876 1877 // StringHeader is the runtime representation of a string. 1878 // It cannot be used safely or portably and its representation may 1879 // change in a later release. 1880 // Moreover, the Data field is not sufficient to guarantee the data 1881 // it references will not be garbage collected, so programs must keep 1882 // a separate, correctly typed pointer to the underlying data. 1883 type StringHeader struct { 1884 Data uintptr 1885 Len int 1886 } 1887 1888 // stringHeader is a safe version of StringHeader used within this package. 1889 type stringHeader struct { 1890 Data unsafe.Pointer 1891 Len int 1892 } 1893 1894 // SliceHeader is the runtime representation of a slice. 1895 // It cannot be used safely or portably and its representation may 1896 // change in a later release. 1897 // Moreover, the Data field is not sufficient to guarantee the data 1898 // it references will not be garbage collected, so programs must keep 1899 // a separate, correctly typed pointer to the underlying data. 1900 type SliceHeader struct { 1901 Data uintptr 1902 Len int 1903 Cap int 1904 } 1905 1906 // sliceHeader is a safe version of SliceHeader used within this package. 1907 type sliceHeader struct { 1908 Data unsafe.Pointer 1909 Len int 1910 Cap int 1911 } 1912 1913 func typesMustMatch(what string, t1, t2 Type) { 1914 if t1 != t2 { 1915 panic(what + ": " + t1.String() + " != " + t2.String()) 1916 } 1917 } 1918 1919 // arrayAt returns the i-th element of p, 1920 // an array whose elements are eltSize bytes wide. 1921 // The array pointed at by p must have at least i+1 elements: 1922 // it is invalid (but impossible to check here) to pass i >= len, 1923 // because then the result will point outside the array. 1924 // whySafe must explain why i < len. (Passing "i < len" is fine; 1925 // the benefit is to surface this assumption at the call site.) 1926 func arrayAt(p unsafe.Pointer, i int, eltSize uintptr, whySafe string) unsafe.Pointer { 1927 return add(p, uintptr(i)*eltSize, "i < len") 1928 } 1929 1930 // grow grows the slice s so that it can hold extra more values, allocating 1931 // more capacity if needed. It also returns the old and new slice lengths. 1932 func grow(s Value, extra int) (Value, int, int) { 1933 i0 := s.Len() 1934 i1 := i0 + extra 1935 if i1 < i0 { 1936 panic("reflect.Append: slice overflow") 1937 } 1938 m := s.Cap() 1939 if i1 <= m { 1940 return s.Slice(0, i1), i0, i1 1941 } 1942 if m == 0 { 1943 m = extra 1944 } else { 1945 for m < i1 { 1946 if i0 < 1024 { 1947 m += m 1948 } else { 1949 m += m / 4 1950 } 1951 } 1952 } 1953 t := MakeSlice(s.Type(), i1, m) 1954 Copy(t, s) 1955 return t, i0, i1 1956 } 1957 1958 // Append appends the values x to a slice s and returns the resulting slice. 1959 // As in Go, each x's value must be assignable to the slice's element type. 1960 func Append(s Value, x ...Value) Value { 1961 s.mustBe(Slice) 1962 s, i0, i1 := grow(s, len(x)) 1963 for i, j := i0, 0; i < i1; i, j = i+1, j+1 { 1964 s.Index(i).Set(x[j]) 1965 } 1966 return s 1967 } 1968 1969 // AppendSlice appends a slice t to a slice s and returns the resulting slice. 1970 // The slices s and t must have the same element type. 1971 func AppendSlice(s, t Value) Value { 1972 s.mustBe(Slice) 1973 t.mustBe(Slice) 1974 typesMustMatch("reflect.AppendSlice", s.Type().Elem(), t.Type().Elem()) 1975 s, i0, i1 := grow(s, t.Len()) 1976 Copy(s.Slice(i0, i1), t) 1977 return s 1978 } 1979 1980 // Copy copies the contents of src into dst until either 1981 // dst has been filled or src has been exhausted. 1982 // It returns the number of elements copied. 1983 // Dst and src each must have kind Slice or Array, and 1984 // dst and src must have the same element type. 1985 // 1986 // As a special case, src can have kind String if the element type of dst is kind Uint8. 1987 func Copy(dst, src Value) int { 1988 dk := dst.kind() 1989 if dk != Array && dk != Slice { 1990 panic(&ValueError{"reflect.Copy", dk}) 1991 } 1992 if dk == Array { 1993 dst.mustBeAssignable() 1994 } 1995 dst.mustBeExported() 1996 1997 sk := src.kind() 1998 var stringCopy bool 1999 if sk != Array && sk != Slice { 2000 stringCopy = sk == String && dst.typ.Elem().Kind() == Uint8 2001 if !stringCopy { 2002 panic(&ValueError{"reflect.Copy", sk}) 2003 } 2004 } 2005 src.mustBeExported() 2006 2007 de := dst.typ.Elem() 2008 if !stringCopy { 2009 se := src.typ.Elem() 2010 typesMustMatch("reflect.Copy", de, se) 2011 } 2012 2013 var ds, ss sliceHeader 2014 if dk == Array { 2015 ds.Data = dst.ptr 2016 ds.Len = dst.Len() 2017 ds.Cap = ds.Len 2018 } else { 2019 ds = *(*sliceHeader)(dst.ptr) 2020 } 2021 if sk == Array { 2022 ss.Data = src.ptr 2023 ss.Len = src.Len() 2024 ss.Cap = ss.Len 2025 } else if sk == Slice { 2026 ss = *(*sliceHeader)(src.ptr) 2027 } else { 2028 sh := *(*stringHeader)(src.ptr) 2029 ss.Data = sh.Data 2030 ss.Len = sh.Len 2031 ss.Cap = sh.Len 2032 } 2033 2034 return typedslicecopy(de.common(), ds, ss) 2035 } 2036 2037 // A runtimeSelect is a single case passed to rselect. 2038 // This must match ../runtime/select.go:/runtimeSelect 2039 type runtimeSelect struct { 2040 dir SelectDir // SelectSend, SelectRecv or SelectDefault 2041 typ *rtype // channel type 2042 ch unsafe.Pointer // channel 2043 val unsafe.Pointer // ptr to data (SendDir) or ptr to receive buffer (RecvDir) 2044 } 2045 2046 // rselect runs a select. It returns the index of the chosen case. 2047 // If the case was a receive, val is filled in with the received value. 2048 // The conventional OK bool indicates whether the receive corresponds 2049 // to a sent value. 2050 //go:noescape 2051 func rselect([]runtimeSelect) (chosen int, recvOK bool) 2052 2053 // A SelectDir describes the communication direction of a select case. 2054 type SelectDir int 2055 2056 // NOTE: These values must match ../runtime/select.go:/selectDir. 2057 2058 const ( 2059 _ SelectDir = iota 2060 SelectSend // case Chan <- Send 2061 SelectRecv // case <-Chan: 2062 SelectDefault // default 2063 ) 2064 2065 // A SelectCase describes a single case in a select operation. 2066 // The kind of case depends on Dir, the communication direction. 2067 // 2068 // If Dir is SelectDefault, the case represents a default case. 2069 // Chan and Send must be zero Values. 2070 // 2071 // If Dir is SelectSend, the case represents a send operation. 2072 // Normally Chan's underlying value must be a channel, and Send's underlying value must be 2073 // assignable to the channel's element type. As a special case, if Chan is a zero Value, 2074 // then the case is ignored, and the field Send will also be ignored and may be either zero 2075 // or non-zero. 2076 // 2077 // If Dir is SelectRecv, the case represents a receive operation. 2078 // Normally Chan's underlying value must be a channel and Send must be a zero Value. 2079 // If Chan is a zero Value, then the case is ignored, but Send must still be a zero Value. 2080 // When a receive operation is selected, the received Value is returned by Select. 2081 // 2082 type SelectCase struct { 2083 Dir SelectDir // direction of case 2084 Chan Value // channel to use (for send or receive) 2085 Send Value // value to send (for send) 2086 } 2087 2088 // Select executes a select operation described by the list of cases. 2089 // Like the Go select statement, it blocks until at least one of the cases 2090 // can proceed, makes a uniform pseudo-random choice, 2091 // and then executes that case. It returns the index of the chosen case 2092 // and, if that case was a receive operation, the value received and a 2093 // boolean indicating whether the value corresponds to a send on the channel 2094 // (as opposed to a zero value received because the channel is closed). 2095 func Select(cases []SelectCase) (chosen int, recv Value, recvOK bool) { 2096 // NOTE: Do not trust that caller is not modifying cases data underfoot. 2097 // The range is safe because the caller cannot modify our copy of the len 2098 // and each iteration makes its own copy of the value c. 2099 runcases := make([]runtimeSelect, len(cases)) 2100 haveDefault := false 2101 for i, c := range cases { 2102 rc := &runcases[i] 2103 rc.dir = c.Dir 2104 switch c.Dir { 2105 default: 2106 panic("reflect.Select: invalid Dir") 2107 2108 case SelectDefault: // default 2109 if haveDefault { 2110 panic("reflect.Select: multiple default cases") 2111 } 2112 haveDefault = true 2113 if c.Chan.IsValid() { 2114 panic("reflect.Select: default case has Chan value") 2115 } 2116 if c.Send.IsValid() { 2117 panic("reflect.Select: default case has Send value") 2118 } 2119 2120 case SelectSend: 2121 ch := c.Chan 2122 if !ch.IsValid() { 2123 break 2124 } 2125 ch.mustBe(Chan) 2126 ch.mustBeExported() 2127 tt := (*chanType)(unsafe.Pointer(ch.typ)) 2128 if ChanDir(tt.dir)&SendDir == 0 { 2129 panic("reflect.Select: SendDir case using recv-only channel") 2130 } 2131 rc.ch = ch.pointer() 2132 rc.typ = &tt.rtype 2133 v := c.Send 2134 if !v.IsValid() { 2135 panic("reflect.Select: SendDir case missing Send value") 2136 } 2137 v.mustBeExported() 2138 v = v.assignTo("reflect.Select", tt.elem, nil) 2139 if v.flag&flagIndir != 0 { 2140 rc.val = v.ptr 2141 } else { 2142 rc.val = unsafe.Pointer(&v.ptr) 2143 } 2144 2145 case SelectRecv: 2146 if c.Send.IsValid() { 2147 panic("reflect.Select: RecvDir case has Send value") 2148 } 2149 ch := c.Chan 2150 if !ch.IsValid() { 2151 break 2152 } 2153 ch.mustBe(Chan) 2154 ch.mustBeExported() 2155 tt := (*chanType)(unsafe.Pointer(ch.typ)) 2156 if ChanDir(tt.dir)&RecvDir == 0 { 2157 panic("reflect.Select: RecvDir case using send-only channel") 2158 } 2159 rc.ch = ch.pointer() 2160 rc.typ = &tt.rtype 2161 rc.val = unsafe_New(tt.elem) 2162 } 2163 } 2164 2165 chosen, recvOK = rselect(runcases) 2166 if runcases[chosen].dir == SelectRecv { 2167 tt := (*chanType)(unsafe.Pointer(runcases[chosen].typ)) 2168 t := tt.elem 2169 p := runcases[chosen].val 2170 fl := flag(t.Kind()) 2171 if ifaceIndir(t) { 2172 recv = Value{t, p, fl | flagIndir} 2173 } else { 2174 recv = Value{t, *(*unsafe.Pointer)(p), fl} 2175 } 2176 } 2177 return chosen, recv, recvOK 2178 } 2179 2180 /* 2181 * constructors 2182 */ 2183 2184 // implemented in package runtime 2185 func unsafe_New(*rtype) unsafe.Pointer 2186 func unsafe_NewArray(*rtype, int) unsafe.Pointer 2187 2188 // MakeSlice creates a new zero-initialized slice value 2189 // for the specified slice type, length, and capacity. 2190 func MakeSlice(typ Type, len, cap int) Value { 2191 if typ.Kind() != Slice { 2192 panic("reflect.MakeSlice of non-slice type") 2193 } 2194 if len < 0 { 2195 panic("reflect.MakeSlice: negative len") 2196 } 2197 if cap < 0 { 2198 panic("reflect.MakeSlice: negative cap") 2199 } 2200 if len > cap { 2201 panic("reflect.MakeSlice: len > cap") 2202 } 2203 2204 s := sliceHeader{unsafe_NewArray(typ.Elem().(*rtype), cap), len, cap} 2205 return Value{typ.(*rtype), unsafe.Pointer(&s), flagIndir | flag(Slice)} 2206 } 2207 2208 // MakeChan creates a new channel with the specified type and buffer size. 2209 func MakeChan(typ Type, buffer int) Value { 2210 if typ.Kind() != Chan { 2211 panic("reflect.MakeChan of non-chan type") 2212 } 2213 if buffer < 0 { 2214 panic("reflect.MakeChan: negative buffer size") 2215 } 2216 if typ.ChanDir() != BothDir { 2217 panic("reflect.MakeChan: unidirectional channel type") 2218 } 2219 t := typ.(*rtype) 2220 ch := makechan(t, buffer) 2221 return Value{t, ch, flag(Chan)} 2222 } 2223 2224 // MakeMap creates a new map with the specified type. 2225 func MakeMap(typ Type) Value { 2226 return MakeMapWithSize(typ, 0) 2227 } 2228 2229 // MakeMapWithSize creates a new map with the specified type 2230 // and initial space for approximately n elements. 2231 func MakeMapWithSize(typ Type, n int) Value { 2232 if typ.Kind() != Map { 2233 panic("reflect.MakeMapWithSize of non-map type") 2234 } 2235 t := typ.(*rtype) 2236 m := makemap(t, n) 2237 return Value{t, m, flag(Map)} 2238 } 2239 2240 // Indirect returns the value that v points to. 2241 // If v is a nil pointer, Indirect returns a zero Value. 2242 // If v is not a pointer, Indirect returns v. 2243 func Indirect(v Value) Value { 2244 if v.Kind() != Ptr { 2245 return v 2246 } 2247 return v.Elem() 2248 } 2249 2250 // ValueOf returns a new Value initialized to the concrete value 2251 // stored in the interface i. ValueOf(nil) returns the zero Value. 2252 func ValueOf(i interface{}) Value { 2253 if i == nil { 2254 return Value{} 2255 } 2256 2257 // TODO: Maybe allow contents of a Value to live on the stack. 2258 // For now we make the contents always escape to the heap. It 2259 // makes life easier in a few places (see chanrecv/mapassign 2260 // comment below). 2261 escapes(i) 2262 2263 return unpackEface(i) 2264 } 2265 2266 // Zero returns a Value representing the zero value for the specified type. 2267 // The result is different from the zero value of the Value struct, 2268 // which represents no value at all. 2269 // For example, Zero(TypeOf(42)) returns a Value with Kind Int and value 0. 2270 // The returned value is neither addressable nor settable. 2271 func Zero(typ Type) Value { 2272 if typ == nil { 2273 panic("reflect: Zero(nil)") 2274 } 2275 t := typ.(*rtype) 2276 fl := flag(t.Kind()) 2277 if ifaceIndir(t) { 2278 return Value{t, unsafe_New(t), fl | flagIndir} 2279 } 2280 return Value{t, nil, fl} 2281 } 2282 2283 // New returns a Value representing a pointer to a new zero value 2284 // for the specified type. That is, the returned Value's Type is PtrTo(typ). 2285 func New(typ Type) Value { 2286 if typ == nil { 2287 panic("reflect: New(nil)") 2288 } 2289 t := typ.(*rtype) 2290 ptr := unsafe_New(t) 2291 fl := flag(Ptr) 2292 return Value{t.ptrTo(), ptr, fl} 2293 } 2294 2295 // NewAt returns a Value representing a pointer to a value of the 2296 // specified type, using p as that pointer. 2297 func NewAt(typ Type, p unsafe.Pointer) Value { 2298 fl := flag(Ptr) 2299 t := typ.(*rtype) 2300 return Value{t.ptrTo(), p, fl} 2301 } 2302 2303 // assignTo returns a value v that can be assigned directly to typ. 2304 // It panics if v is not assignable to typ. 2305 // For a conversion to an interface type, target is a suggested scratch space to use. 2306 func (v Value) assignTo(context string, dst *rtype, target unsafe.Pointer) Value { 2307 if v.flag&flagMethod != 0 { 2308 v = makeMethodValue(context, v) 2309 } 2310 2311 switch { 2312 case directlyAssignable(dst, v.typ): 2313 // Overwrite type so that they match. 2314 // Same memory layout, so no harm done. 2315 fl := v.flag&(flagAddr|flagIndir) | v.flag.ro() 2316 fl |= flag(dst.Kind()) 2317 return Value{dst, v.ptr, fl} 2318 2319 case implements(dst, v.typ): 2320 if target == nil { 2321 target = unsafe_New(dst) 2322 } 2323 if v.Kind() == Interface && v.IsNil() { 2324 // A nil ReadWriter passed to nil Reader is OK, 2325 // but using ifaceE2I below will panic. 2326 // Avoid the panic by returning a nil dst (e.g., Reader) explicitly. 2327 return Value{dst, nil, flag(Interface)} 2328 } 2329 x := valueInterface(v, false) 2330 if dst.NumMethod() == 0 { 2331 *(*interface{})(target) = x 2332 } else { 2333 ifaceE2I(dst, x, target) 2334 } 2335 return Value{dst, target, flagIndir | flag(Interface)} 2336 } 2337 2338 // Failed. 2339 panic(context + ": value of type " + v.typ.String() + " is not assignable to type " + dst.String()) 2340 } 2341 2342 // Convert returns the value v converted to type t. 2343 // If the usual Go conversion rules do not allow conversion 2344 // of the value v to type t, Convert panics. 2345 func (v Value) Convert(t Type) Value { 2346 if v.flag&flagMethod != 0 { 2347 v = makeMethodValue("Convert", v) 2348 } 2349 op := convertOp(t.common(), v.typ) 2350 if op == nil { 2351 panic("reflect.Value.Convert: value of type " + v.typ.String() + " cannot be converted to type " + t.String()) 2352 } 2353 return op(v, t) 2354 } 2355 2356 // convertOp returns the function to convert a value of type src 2357 // to a value of type dst. If the conversion is illegal, convertOp returns nil. 2358 func convertOp(dst, src *rtype) func(Value, Type) Value { 2359 switch src.Kind() { 2360 case Int, Int8, Int16, Int32, Int64: 2361 switch dst.Kind() { 2362 case Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64, Uintptr: 2363 return cvtInt 2364 case Float32, Float64: 2365 return cvtIntFloat 2366 case String: 2367 return cvtIntString 2368 } 2369 2370 case Uint, Uint8, Uint16, Uint32, Uint64, Uintptr: 2371 switch dst.Kind() { 2372 case Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64, Uintptr: 2373 return cvtUint 2374 case Float32, Float64: 2375 return cvtUintFloat 2376 case String: 2377 return cvtUintString 2378 } 2379 2380 case Float32, Float64: 2381 switch dst.Kind() { 2382 case Int, Int8, Int16, Int32, Int64: 2383 return cvtFloatInt 2384 case Uint, Uint8, Uint16, Uint32, Uint64, Uintptr: 2385 return cvtFloatUint 2386 case Float32, Float64: 2387 return cvtFloat 2388 } 2389 2390 case Complex64, Complex128: 2391 switch dst.Kind() { 2392 case Complex64, Complex128: 2393 return cvtComplex 2394 } 2395 2396 case String: 2397 if dst.Kind() == Slice && dst.Elem().PkgPath() == "" { 2398 switch dst.Elem().Kind() { 2399 case Uint8: 2400 return cvtStringBytes 2401 case Int32: 2402 return cvtStringRunes 2403 } 2404 } 2405 2406 case Slice: 2407 if dst.Kind() == String && src.Elem().PkgPath() == "" { 2408 switch src.Elem().Kind() { 2409 case Uint8: 2410 return cvtBytesString 2411 case Int32: 2412 return cvtRunesString 2413 } 2414 } 2415 } 2416 2417 // dst and src have same underlying type. 2418 if haveIdenticalUnderlyingType(dst, src, false) { 2419 return cvtDirect 2420 } 2421 2422 // dst and src are non-defined pointer types with same underlying base type. 2423 if dst.Kind() == Ptr && dst.Name() == "" && 2424 src.Kind() == Ptr && src.Name() == "" && 2425 haveIdenticalUnderlyingType(dst.Elem().common(), src.Elem().common(), false) { 2426 return cvtDirect 2427 } 2428 2429 if implements(dst, src) { 2430 if src.Kind() == Interface { 2431 return cvtI2I 2432 } 2433 return cvtT2I 2434 } 2435 2436 return nil 2437 } 2438 2439 // makeInt returns a Value of type t equal to bits (possibly truncated), 2440 // where t is a signed or unsigned int type. 2441 func makeInt(f flag, bits uint64, t Type) Value { 2442 typ := t.common() 2443 ptr := unsafe_New(typ) 2444 switch typ.size { 2445 case 1: 2446 *(*uint8)(ptr) = uint8(bits) 2447 case 2: 2448 *(*uint16)(ptr) = uint16(bits) 2449 case 4: 2450 *(*uint32)(ptr) = uint32(bits) 2451 case 8: 2452 *(*uint64)(ptr) = bits 2453 } 2454 return Value{typ, ptr, f | flagIndir | flag(typ.Kind())} 2455 } 2456 2457 // makeFloat returns a Value of type t equal to v (possibly truncated to float32), 2458 // where t is a float32 or float64 type. 2459 func makeFloat(f flag, v float64, t Type) Value { 2460 typ := t.common() 2461 ptr := unsafe_New(typ) 2462 switch typ.size { 2463 case 4: 2464 *(*float32)(ptr) = float32(v) 2465 case 8: 2466 *(*float64)(ptr) = v 2467 } 2468 return Value{typ, ptr, f | flagIndir | flag(typ.Kind())} 2469 } 2470 2471 // makeComplex returns a Value of type t equal to v (possibly truncated to complex64), 2472 // where t is a complex64 or complex128 type. 2473 func makeComplex(f flag, v complex128, t Type) Value { 2474 typ := t.common() 2475 ptr := unsafe_New(typ) 2476 switch typ.size { 2477 case 8: 2478 *(*complex64)(ptr) = complex64(v) 2479 case 16: 2480 *(*complex128)(ptr) = v 2481 } 2482 return Value{typ, ptr, f | flagIndir | flag(typ.Kind())} 2483 } 2484 2485 func makeString(f flag, v string, t Type) Value { 2486 ret := New(t).Elem() 2487 ret.SetString(v) 2488 ret.flag = ret.flag&^flagAddr | f 2489 return ret 2490 } 2491 2492 func makeBytes(f flag, v []byte, t Type) Value { 2493 ret := New(t).Elem() 2494 ret.SetBytes(v) 2495 ret.flag = ret.flag&^flagAddr | f 2496 return ret 2497 } 2498 2499 func makeRunes(f flag, v []rune, t Type) Value { 2500 ret := New(t).Elem() 2501 ret.setRunes(v) 2502 ret.flag = ret.flag&^flagAddr | f 2503 return ret 2504 } 2505 2506 // These conversion functions are returned by convertOp 2507 // for classes of conversions. For example, the first function, cvtInt, 2508 // takes any value v of signed int type and returns the value converted 2509 // to type t, where t is any signed or unsigned int type. 2510 2511 // convertOp: intXX -> [u]intXX 2512 func cvtInt(v Value, t Type) Value { 2513 return makeInt(v.flag.ro(), uint64(v.Int()), t) 2514 } 2515 2516 // convertOp: uintXX -> [u]intXX 2517 func cvtUint(v Value, t Type) Value { 2518 return makeInt(v.flag.ro(), v.Uint(), t) 2519 } 2520 2521 // convertOp: floatXX -> intXX 2522 func cvtFloatInt(v Value, t Type) Value { 2523 return makeInt(v.flag.ro(), uint64(int64(v.Float())), t) 2524 } 2525 2526 // convertOp: floatXX -> uintXX 2527 func cvtFloatUint(v Value, t Type) Value { 2528 return makeInt(v.flag.ro(), uint64(v.Float()), t) 2529 } 2530 2531 // convertOp: intXX -> floatXX 2532 func cvtIntFloat(v Value, t Type) Value { 2533 return makeFloat(v.flag.ro(), float64(v.Int()), t) 2534 } 2535 2536 // convertOp: uintXX -> floatXX 2537 func cvtUintFloat(v Value, t Type) Value { 2538 return makeFloat(v.flag.ro(), float64(v.Uint()), t) 2539 } 2540 2541 // convertOp: floatXX -> floatXX 2542 func cvtFloat(v Value, t Type) Value { 2543 return makeFloat(v.flag.ro(), v.Float(), t) 2544 } 2545 2546 // convertOp: complexXX -> complexXX 2547 func cvtComplex(v Value, t Type) Value { 2548 return makeComplex(v.flag.ro(), v.Complex(), t) 2549 } 2550 2551 // convertOp: intXX -> string 2552 func cvtIntString(v Value, t Type) Value { 2553 return makeString(v.flag.ro(), string(v.Int()), t) 2554 } 2555 2556 // convertOp: uintXX -> string 2557 func cvtUintString(v Value, t Type) Value { 2558 return makeString(v.flag.ro(), string(v.Uint()), t) 2559 } 2560 2561 // convertOp: []byte -> string 2562 func cvtBytesString(v Value, t Type) Value { 2563 return makeString(v.flag.ro(), string(v.Bytes()), t) 2564 } 2565 2566 // convertOp: string -> []byte 2567 func cvtStringBytes(v Value, t Type) Value { 2568 return makeBytes(v.flag.ro(), []byte(v.String()), t) 2569 } 2570 2571 // convertOp: []rune -> string 2572 func cvtRunesString(v Value, t Type) Value { 2573 return makeString(v.flag.ro(), string(v.runes()), t) 2574 } 2575 2576 // convertOp: string -> []rune 2577 func cvtStringRunes(v Value, t Type) Value { 2578 return makeRunes(v.flag.ro(), []rune(v.String()), t) 2579 } 2580 2581 // convertOp: direct copy 2582 func cvtDirect(v Value, typ Type) Value { 2583 f := v.flag 2584 t := typ.common() 2585 ptr := v.ptr 2586 if f&flagAddr != 0 { 2587 // indirect, mutable word - make a copy 2588 c := unsafe_New(t) 2589 typedmemmove(t, c, ptr) 2590 ptr = c 2591 f &^= flagAddr 2592 } 2593 return Value{t, ptr, v.flag.ro() | f} // v.flag.ro()|f == f? 2594 } 2595 2596 // convertOp: concrete -> interface 2597 func cvtT2I(v Value, typ Type) Value { 2598 target := unsafe_New(typ.common()) 2599 x := valueInterface(v, false) 2600 if typ.NumMethod() == 0 { 2601 *(*interface{})(target) = x 2602 } else { 2603 ifaceE2I(typ.(*rtype), x, target) 2604 } 2605 return Value{typ.common(), target, v.flag.ro() | flagIndir | flag(Interface)} 2606 } 2607 2608 // convertOp: interface -> interface 2609 func cvtI2I(v Value, typ Type) Value { 2610 if v.IsNil() { 2611 ret := Zero(typ) 2612 ret.flag |= v.flag.ro() 2613 return ret 2614 } 2615 return cvtT2I(v.Elem(), typ) 2616 } 2617 2618 // implemented in ../runtime 2619 func chancap(ch unsafe.Pointer) int 2620 func chanclose(ch unsafe.Pointer) 2621 func chanlen(ch unsafe.Pointer) int 2622 2623 // Note: some of the noescape annotations below are technically a lie, 2624 // but safe in the context of this package. Functions like chansend 2625 // and mapassign don't escape the referent, but may escape anything 2626 // the referent points to (they do shallow copies of the referent). 2627 // It is safe in this package because the referent may only point 2628 // to something a Value may point to, and that is always in the heap 2629 // (due to the escapes() call in ValueOf). 2630 2631 //go:noescape 2632 func chanrecv(ch unsafe.Pointer, nb bool, val unsafe.Pointer) (selected, received bool) 2633 2634 //go:noescape 2635 func chansend(ch unsafe.Pointer, val unsafe.Pointer, nb bool) bool 2636 2637 func makechan(typ *rtype, size int) (ch unsafe.Pointer) 2638 func makemap(t *rtype, cap int) (m unsafe.Pointer) 2639 2640 //go:noescape 2641 func mapaccess(t *rtype, m unsafe.Pointer, key unsafe.Pointer) (val unsafe.Pointer) 2642 2643 //go:noescape 2644 func mapassign(t *rtype, m unsafe.Pointer, key, val unsafe.Pointer) 2645 2646 //go:noescape 2647 func mapdelete(t *rtype, m unsafe.Pointer, key unsafe.Pointer) 2648 2649 // m escapes into the return value, but the caller of mapiterinit 2650 // doesn't let the return value escape. 2651 //go:noescape 2652 func mapiterinit(t *rtype, m unsafe.Pointer) unsafe.Pointer 2653 2654 //go:noescape 2655 func mapiterkey(it unsafe.Pointer) (key unsafe.Pointer) 2656 2657 //go:noescape 2658 func mapitervalue(it unsafe.Pointer) (value unsafe.Pointer) 2659 2660 //go:noescape 2661 func mapiternext(it unsafe.Pointer) 2662 2663 //go:noescape 2664 func maplen(m unsafe.Pointer) int 2665 2666 // call calls fn with a copy of the n argument bytes pointed at by arg. 2667 // After fn returns, reflectcall copies n-retoffset result bytes 2668 // back into arg+retoffset before returning. If copying result bytes back, 2669 // the caller must pass the argument frame type as argtype, so that 2670 // call can execute appropriate write barriers during the copy. 2671 // 2672 //go:linkname call runtime.reflectcall 2673 func call(argtype *rtype, fn, arg unsafe.Pointer, n uint32, retoffset uint32) 2674 2675 func ifaceE2I(t *rtype, src interface{}, dst unsafe.Pointer) 2676 2677 // memmove copies size bytes to dst from src. No write barriers are used. 2678 //go:noescape 2679 func memmove(dst, src unsafe.Pointer, size uintptr) 2680 2681 // typedmemmove copies a value of type t to dst from src. 2682 //go:noescape 2683 func typedmemmove(t *rtype, dst, src unsafe.Pointer) 2684 2685 // typedmemmovepartial is like typedmemmove but assumes that 2686 // dst and src point off bytes into the value and only copies size bytes. 2687 //go:noescape 2688 func typedmemmovepartial(t *rtype, dst, src unsafe.Pointer, off, size uintptr) 2689 2690 // typedmemclr zeros the value at ptr of type t. 2691 //go:noescape 2692 func typedmemclr(t *rtype, ptr unsafe.Pointer) 2693 2694 // typedmemclrpartial is like typedmemclr but assumes that 2695 // dst points off bytes into the value and only clears size bytes. 2696 //go:noescape 2697 func typedmemclrpartial(t *rtype, ptr unsafe.Pointer, off, size uintptr) 2698 2699 // typedslicecopy copies a slice of elemType values from src to dst, 2700 // returning the number of elements copied. 2701 //go:noescape 2702 func typedslicecopy(elemType *rtype, dst, src sliceHeader) int 2703 2704 // Dummy annotation marking that the value x escapes, 2705 // for use in cases where the reflect code is so clever that 2706 // the compiler cannot follow. 2707 func escapes(x interface{}) { 2708 if dummy.b { 2709 dummy.x = x 2710 } 2711 } 2712 2713 var dummy struct { 2714 b bool 2715 x interface{} 2716 }