| 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 |
package reflect |
| 17 |
|
| 18 |
import ( |
| 19 |
"internal/abi" |
| 20 |
"internal/goarch" |
| 21 |
"runtime" |
| 22 |
"strconv" |
| 23 |
"sync" |
| 24 |
"unicode" |
| 25 |
"unicode/utf8" |
| 26 |
"unsafe" |
| 27 |
) |
| 28 |
|
| 29 |
// Type is the representation of a Go type. |
| 30 |
// |
| 31 |
// Not all methods apply to all kinds of types. Restrictions, |
| 32 |
// if any, are noted in the documentation for each method. |
| 33 |
// Use the Kind method to find out the kind of type before |
| 34 |
// calling kind-specific methods. Calling a method |
| 35 |
// inappropriate to the kind of type causes a run-time panic. |
| 36 |
// |
| 37 |
// Type values are comparable, such as with the == operator, |
| 38 |
// so they can be used as map keys. |
| 39 |
// Two Type values are equal if they represent identical types. |
| 40 |
type Type interface { |
| 41 |
// Methods applicable to all types. |
| 42 |
|
| 43 |
// Align returns the alignment in bytes of a value of |
| 44 |
// this type when allocated in memory. |
| 45 |
Align() int |
| 46 |
|
| 47 |
// FieldAlign returns the alignment in bytes of a value of |
| 48 |
// this type when used as a field in a struct. |
| 49 |
FieldAlign() int |
| 50 |
|
| 51 |
// Method returns the i'th method in the type's method set. |
| 52 |
// It panics if i is not in the range [0, NumMethod()). |
| 53 |
// |
| 54 |
// For a non-interface type T or *T, the returned Method's Type and Func |
| 55 |
// fields describe a function whose first argument is the receiver, |
| 56 |
// and only exported methods are accessible. |
| 57 |
// |
| 58 |
// For an interface type, the returned Method's Type field gives the |
| 59 |
// method signature, without a receiver, and the Func field is nil. |
| 60 |
// |
| 61 |
// Methods are sorted in lexicographic order. |
| 62 |
// |
| 63 |
// Calling this method will force the linker to retain all exported methods in all packages. |
| 64 |
// This may make the executable binary larger but will not affect execution time. |
| 65 |
Method(int) Method |
| 66 |
|
| 67 |
// MethodByName returns the method with that name in the type's |
| 68 |
// method set and a boolean indicating if the method was found. |
| 69 |
// |
| 70 |
// For a non-interface type T or *T, the returned Method's Type and Func |
| 71 |
// fields describe a function whose first argument is the receiver. |
| 72 |
// |
| 73 |
// For an interface type, the returned Method's Type field gives the |
| 74 |
// method signature, without a receiver, and the Func field is nil. |
| 75 |
// |
| 76 |
// Calling this method will cause the linker to retain all methods with this name in all packages. |
| 77 |
// If the linker can't determine the name, it will retain all exported methods. |
| 78 |
// This may make the executable binary larger but will not affect execution time. |
| 79 |
MethodByName(string) (Method, bool) |
| 80 |
|
| 81 |
// NumMethod returns the number of methods accessible using Method. |
| 82 |
// |
| 83 |
// For a non-interface type, it returns the number of exported methods. |
| 84 |
// |
| 85 |
// For an interface type, it returns the number of exported and unexported methods. |
| 86 |
NumMethod() int |
| 87 |
|
| 88 |
// Name returns the type's name within its package for a defined type. |
| 89 |
// For other (non-defined) types it returns the empty string. |
| 90 |
Name() string |
| 91 |
|
| 92 |
// PkgPath returns a defined type's package path, that is, the import path |
| 93 |
// that uniquely identifies the package, such as "encoding/base64". |
| 94 |
// If the type was predeclared (string, error) or not defined (*T, struct{}, |
| 95 |
// []int, or A where A is an alias for a non-defined type), the package path |
| 96 |
// will be the empty string. |
| 97 |
PkgPath() string |
| 98 |
|
| 99 |
// Size returns the number of bytes needed to store |
| 100 |
// a value of the given type; it is analogous to unsafe.Sizeof. |
| 101 |
Size() uintptr |
| 102 |
|
| 103 |
// String returns a string representation of the type. |
| 104 |
// The string representation may use shortened package names |
| 105 |
// (e.g., base64 instead of "encoding/base64") and is not |
| 106 |
// guaranteed to be unique among types. To test for type identity, |
| 107 |
// compare the Types directly. |
| 108 |
String() string |
| 109 |
|
| 110 |
// Kind returns the specific kind of this type. |
| 111 |
Kind() Kind |
| 112 |
|
| 113 |
// Implements reports whether the type implements the interface type u. |
| 114 |
Implements(u Type) bool |
| 115 |
|
| 116 |
// AssignableTo reports whether a value of the type is assignable to type u. |
| 117 |
AssignableTo(u Type) bool |
| 118 |
|
| 119 |
// ConvertibleTo reports whether a value of the type is convertible to type u. |
| 120 |
// Even if ConvertibleTo returns true, the conversion may still panic. |
| 121 |
// For example, a slice of type []T is convertible to *[N]T, |
| 122 |
// but the conversion will panic if its length is less than N. |
| 123 |
ConvertibleTo(u Type) bool |
| 124 |
|
| 125 |
// Comparable reports whether values of this type are comparable. |
| 126 |
// Even if Comparable returns true, the comparison may still panic. |
| 127 |
// For example, values of interface type are comparable, |
| 128 |
// but the comparison will panic if their dynamic type is not comparable. |
| 129 |
Comparable() bool |
| 130 |
|
| 131 |
// Methods applicable only to some types, depending on Kind. |
| 132 |
// The methods allowed for each kind are: |
| 133 |
// |
| 134 |
// Int*, Uint*, Float*, Complex*: Bits |
| 135 |
// Array: Elem, Len |
| 136 |
// Chan: ChanDir, Elem |
| 137 |
// Func: In, NumIn, Out, NumOut, IsVariadic. |
| 138 |
// Map: Key, Elem |
| 139 |
// Pointer: Elem |
| 140 |
// Slice: Elem |
| 141 |
// Struct: Field, FieldByIndex, FieldByName, FieldByNameFunc, NumField |
| 142 |
|
| 143 |
// Bits returns the size of the type in bits. |
| 144 |
// It panics if the type's Kind is not one of the |
| 145 |
// sized or unsized Int, Uint, Float, or Complex kinds. |
| 146 |
Bits() int |
| 147 |
|
| 148 |
// ChanDir returns a channel type's direction. |
| 149 |
// It panics if the type's Kind is not Chan. |
| 150 |
ChanDir() ChanDir |
| 151 |
|
| 152 |
// IsVariadic reports whether a function type's final input parameter |
| 153 |
// is a "..." parameter. If so, t.In(t.NumIn() - 1) returns the parameter's |
| 154 |
// implicit actual type []T. |
| 155 |
// |
| 156 |
// For concreteness, if t represents func(x int, y ... float64), then |
| 157 |
// |
| 158 |
// t.NumIn() == 2 |
| 159 |
// t.In(0) is the reflect.Type for "int" |
| 160 |
// t.In(1) is the reflect.Type for "[]float64" |
| 161 |
// t.IsVariadic() == true |
| 162 |
// |
| 163 |
// IsVariadic panics if the type's Kind is not Func. |
| 164 |
IsVariadic() bool |
| 165 |
|
| 166 |
// Elem returns a type's element type. |
| 167 |
// It panics if the type's Kind is not Array, Chan, Map, Pointer, or Slice. |
| 168 |
Elem() Type |
| 169 |
|
| 170 |
// Field returns a struct type's i'th field. |
| 171 |
// It panics if the type's Kind is not Struct. |
| 172 |
// It panics if i is not in the range [0, NumField()). |
| 173 |
Field(i int) StructField |
| 174 |
|
| 175 |
// FieldByIndex returns the nested field corresponding |
| 176 |
// to the index sequence. It is equivalent to calling Field |
| 177 |
// successively for each index i. |
| 178 |
// It panics if the type's Kind is not Struct. |
| 179 |
FieldByIndex(index []int) StructField |
| 180 |
|
| 181 |
// FieldByName returns the struct field with the given name |
| 182 |
// and a boolean indicating if the field was found. |
| 183 |
// If the returned field is promoted from an embedded struct, |
| 184 |
// then Offset in the returned StructField is the offset in |
| 185 |
// the embedded struct. |
| 186 |
FieldByName(name string) (StructField, bool) |
| 187 |
|
| 188 |
// FieldByNameFunc returns the struct field with a name |
| 189 |
// that satisfies the match function and a boolean indicating if |
| 190 |
// the field was found. |
| 191 |
// |
| 192 |
// FieldByNameFunc considers the fields in the struct itself |
| 193 |
// and then the fields in any embedded structs, in breadth first order, |
| 194 |
// stopping at the shallowest nesting depth containing one or more |
| 195 |
// fields satisfying the match function. If multiple fields at that depth |
| 196 |
// satisfy the match function, they cancel each other |
| 197 |
// and FieldByNameFunc returns no match. |
| 198 |
// This behavior mirrors Go's handling of name lookup in |
| 199 |
// structs containing embedded fields. |
| 200 |
// |
| 201 |
// If the returned field is promoted from an embedded struct, |
| 202 |
// then Offset in the returned StructField is the offset in |
| 203 |
// the embedded struct. |
| 204 |
FieldByNameFunc(match func(string) bool) (StructField, bool) |
| 205 |
|
| 206 |
// In returns the type of a function type's i'th input parameter. |
| 207 |
// It panics if the type's Kind is not Func. |
| 208 |
// It panics if i is not in the range [0, NumIn()). |
| 209 |
In(i int) Type |
| 210 |
|
| 211 |
// Key returns a map type's key type. |
| 212 |
// It panics if the type's Kind is not Map. |
| 213 |
Key() Type |
| 214 |
|
| 215 |
// Len returns an array type's length. |
| 216 |
// It panics if the type's Kind is not Array. |
| 217 |
Len() int |
| 218 |
|
| 219 |
// NumField returns a struct type's field count. |
| 220 |
// It panics if the type's Kind is not Struct. |
| 221 |
NumField() int |
| 222 |
|
| 223 |
// NumIn returns a function type's input parameter count. |
| 224 |
// It panics if the type's Kind is not Func. |
| 225 |
NumIn() int |
| 226 |
|
| 227 |
// NumOut returns a function type's output parameter count. |
| 228 |
// It panics if the type's Kind is not Func. |
| 229 |
NumOut() int |
| 230 |
|
| 231 |
// Out returns the type of a function type's i'th output parameter. |
| 232 |
// It panics if the type's Kind is not Func. |
| 233 |
// It panics if i is not in the range [0, NumOut()). |
| 234 |
Out(i int) Type |
| 235 |
|
| 236 |
// OverflowComplex reports whether the complex128 x cannot be represented by type t. |
| 237 |
// It panics if t's Kind is not Complex64 or Complex128. |
| 238 |
OverflowComplex(x complex128) bool |
| 239 |
|
| 240 |
// OverflowFloat reports whether the float64 x cannot be represented by type t. |
| 241 |
// It panics if t's Kind is not Float32 or Float64. |
| 242 |
OverflowFloat(x float64) bool |
| 243 |
|
| 244 |
// OverflowInt reports whether the int64 x cannot be represented by type t. |
| 245 |
// It panics if t's Kind is not Int, Int8, Int16, Int32, or Int64. |
| 246 |
OverflowInt(x int64) bool |
| 247 |
|
| 248 |
// OverflowUint reports whether the uint64 x cannot be represented by type t. |
| 249 |
// It panics if t's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64. |
| 250 |
OverflowUint(x uint64) bool |
| 251 |
|
| 252 |
// CanSeq reports whether a [Value] with this type can be iterated over using [Value.Seq]. |
| 253 |
CanSeq() bool |
| 254 |
|
| 255 |
// CanSeq2 reports whether a [Value] with this type can be iterated over using [Value.Seq2]. |
| 256 |
CanSeq2() bool |
| 257 |
|
| 258 |
common() *abi.Type |
| 259 |
uncommon() *uncommonType |
| 260 |
} |
| 261 |
|
| 262 |
// BUG(rsc): FieldByName and related functions consider struct field names to be equal |
| 263 |
// if the names are equal, even if they are unexported names originating |
| 264 |
// in different packages. The practical effect of this is that the result of |
| 265 |
// t.FieldByName("x") is not well defined if the struct type t contains |
| 266 |
// multiple fields named x (embedded from different packages). |
| 267 |
// FieldByName may return one of the fields named x or may report that there are none. |
| 268 |
// See https://golang.org/issue/4876 for more details. |
| 269 |
|
| 270 |
/* |
| 271 |
* These data structures are known to the compiler (../cmd/compile/internal/reflectdata/reflect.go). |
| 272 |
* A few are known to ../runtime/type.go to convey to debuggers. |
| 273 |
* They are also known to ../internal/abi/type.go. |
| 274 |
*/ |
| 275 |
|
| 276 |
// A Kind represents the specific kind of type that a [Type] represents. |
| 277 |
// The zero Kind is not a valid kind. |
| 278 |
type Kind uint |
| 279 |
|
| 280 |
const ( |
| 281 |
Invalid Kind = iota |
| 282 |
Bool |
| 283 |
Int |
| 284 |
Int8 |
| 285 |
Int16 |
| 286 |
Int32 |
| 287 |
Int64 |
| 288 |
Uint |
| 289 |
Uint8 |
| 290 |
Uint16 |
| 291 |
Uint32 |
| 292 |
Uint64 |
| 293 |
Uintptr |
| 294 |
Float32 |
| 295 |
Float64 |
| 296 |
Complex64 |
| 297 |
Complex128 |
| 298 |
Array |
| 299 |
Chan |
| 300 |
Func |
| 301 |
Interface |
| 302 |
Map |
| 303 |
Pointer |
| 304 |
Slice |
| 305 |
String |
| 306 |
Struct |
| 307 |
UnsafePointer |
| 308 |
) |
| 309 |
|
| 310 |
// Ptr is the old name for the [Pointer] kind. |
| 311 |
// |
| 312 |
//go:fix inline |
| 313 |
const Ptr = Pointer |
| 314 |
|
| 315 |
// uncommonType is present only for defined types or types with methods |
| 316 |
// (if T is a defined type, the uncommonTypes for T and *T have methods). |
| 317 |
// Using a pointer to this struct reduces the overall size required |
| 318 |
// to describe a non-defined type with no methods. |
| 319 |
type uncommonType = abi.UncommonType |
| 320 |
|
| 321 |
// Embed this type to get common/uncommon |
| 322 |
type common struct { |
| 323 |
abi.Type |
| 324 |
} |
| 325 |
|
| 326 |
// rtype is the common implementation of most values. |
| 327 |
// It is embedded in other struct types. |
| 328 |
type rtype struct { |
| 329 |
t abi.Type |
| 330 |
} |
| 331 |
|
| 332 |
func (t *rtype) common() *abi.Type { |
| 333 |
return &t.t |
| 334 |
} |
| 335 |
|
| 336 |
func (t *rtype) uncommon() *abi.UncommonType { |
| 337 |
return t.t.Uncommon() |
| 338 |
} |
| 339 |
|
| 340 |
type aNameOff = abi.NameOff |
| 341 |
type aTypeOff = abi.TypeOff |
| 342 |
type aTextOff = abi.TextOff |
| 343 |
|
| 344 |
// ChanDir represents a channel type's direction. |
| 345 |
type ChanDir int |
| 346 |
|
| 347 |
const ( |
| 348 |
RecvDir ChanDir = 1 << iota // <-chan |
| 349 |
SendDir // chan<- |
| 350 |
BothDir = RecvDir | SendDir // chan |
| 351 |
) |
| 352 |
|
| 353 |
// arrayType represents a fixed array type. |
| 354 |
type arrayType = abi.ArrayType |
| 355 |
|
| 356 |
// chanType represents a channel type. |
| 357 |
type chanType = abi.ChanType |
| 358 |
|
| 359 |
// funcType represents a function type. |
| 360 |
// |
| 361 |
// A *rtype for each in and out parameter is stored in an array that |
| 362 |
// directly follows the funcType (and possibly its uncommonType). So |
| 363 |
// a function type with one method, one input, and one output is: |
| 364 |
// |
| 365 |
// struct { |
| 366 |
// funcType |
| 367 |
// uncommonType |
| 368 |
// [2]*rtype // [0] is in, [1] is out |
| 369 |
// } |
| 370 |
type funcType = abi.FuncType |
| 371 |
|
| 372 |
// interfaceType represents an interface type. |
| 373 |
type interfaceType struct { |
| 374 |
abi.InterfaceType // can embed directly because not a public type. |
| 375 |
} |
| 376 |
|
| 377 |
func (t *interfaceType) nameOff(off aNameOff) abi.Name { |
| 378 |
return toRType(&t.Type).nameOff(off) |
| 379 |
} |
| 380 |
|
| 381 |
func nameOffFor(t *abi.Type, off aNameOff) abi.Name { |
| 382 |
return toRType(t).nameOff(off) |
| 383 |
} |
| 384 |
|
| 385 |
func typeOffFor(t *abi.Type, off aTypeOff) *abi.Type { |
| 386 |
return toRType(t).typeOff(off) |
| 387 |
} |
| 388 |
|
| 389 |
func (t *interfaceType) typeOff(off aTypeOff) *abi.Type { |
| 390 |
return toRType(&t.Type).typeOff(off) |
| 391 |
} |
| 392 |
|
| 393 |
func (t *interfaceType) common() *abi.Type { |
| 394 |
return &t.Type |
| 395 |
} |
| 396 |
|
| 397 |
func (t *interfaceType) uncommon() *abi.UncommonType { |
| 398 |
return t.Uncommon() |
| 399 |
} |
| 400 |
|
| 401 |
// ptrType represents a pointer type. |
| 402 |
type ptrType struct { |
| 403 |
abi.PtrType |
| 404 |
} |
| 405 |
|
| 406 |
// sliceType represents a slice type. |
| 407 |
type sliceType struct { |
| 408 |
abi.SliceType |
| 409 |
} |
| 410 |
|
| 411 |
// Struct field |
| 412 |
type structField = abi.StructField |
| 413 |
|
| 414 |
// structType represents a struct type. |
| 415 |
type structType struct { |
| 416 |
abi.StructType |
| 417 |
} |
| 418 |
|
| 419 |
func pkgPath(n abi.Name) string { |
| 420 |
if n.Bytes == nil || *n.DataChecked(0, "name flag field")&(1<<2) == 0 { |
| 421 |
return "" |
| 422 |
} |
| 423 |
i, l := n.ReadVarint(1) |
| 424 |
off := 1 + i + l |
| 425 |
if n.HasTag() { |
| 426 |
i2, l2 := n.ReadVarint(off) |
| 427 |
off += i2 + l2 |
| 428 |
} |
| 429 |
var nameOff int32 |
| 430 |
// Note that this field may not be aligned in memory, |
| 431 |
// so we cannot use a direct int32 assignment here. |
| 432 |
copy((*[4]byte)(unsafe.Pointer(&nameOff))[:], (*[4]byte)(unsafe.Pointer(n.DataChecked(off, "name offset field")))[:]) |
| 433 |
pkgPathName := abi.Name{Bytes: (*byte)(resolveTypeOff(unsafe.Pointer(n.Bytes), nameOff))} |
| 434 |
return pkgPathName.Name() |
| 435 |
} |
| 436 |
|
| 437 |
func newName(n, tag string, exported, embedded bool) abi.Name { |
| 438 |
return abi.NewName(n, tag, exported, embedded) |
| 439 |
} |
| 440 |
|
| 441 |
/* |
| 442 |
* The compiler knows the exact layout of all the data structures above. |
| 443 |
* The compiler does not know about the data structures and methods below. |
| 444 |
*/ |
| 445 |
|
| 446 |
// Method represents a single method. |
| 447 |
type Method struct { |
| 448 |
// Name is the method name. |
| 449 |
Name string |
| 450 |
|
| 451 |
// PkgPath is the package path that qualifies a lower case (unexported) |
| 452 |
// method name. It is empty for upper case (exported) method names. |
| 453 |
// The combination of PkgPath and Name uniquely identifies a method |
| 454 |
// in a method set. |
| 455 |
// See https://golang.org/ref/spec#Uniqueness_of_identifiers |
| 456 |
PkgPath string |
| 457 |
|
| 458 |
Type Type // method type |
| 459 |
Func Value // func with receiver as first argument |
| 460 |
Index int // index for Type.Method |
| 461 |
} |
| 462 |
|
| 463 |
// IsExported reports whether the method is exported. |
| 464 |
func (m Method) IsExported() bool { |
| 465 |
return m.PkgPath == "" |
| 466 |
} |
| 467 |
|
| 468 |
// String returns the name of k. |
| 469 |
func (k Kind) String() string { |
| 470 |
if uint(k) < uint(len(kindNames)) { |
| 471 |
return kindNames[uint(k)] |
| 472 |
} |
| 473 |
return "kind" + strconv.Itoa(int(k)) |
| 474 |
} |
| 475 |
|
| 476 |
var kindNames = []string{ |
| 477 |
Invalid: "invalid", |
| 478 |
Bool: "bool", |
| 479 |
Int: "int", |
| 480 |
Int8: "int8", |
| 481 |
Int16: "int16", |
| 482 |
Int32: "int32", |
| 483 |
Int64: "int64", |
| 484 |
Uint: "uint", |
| 485 |
Uint8: "uint8", |
| 486 |
Uint16: "uint16", |
| 487 |
Uint32: "uint32", |
| 488 |
Uint64: "uint64", |
| 489 |
Uintptr: "uintptr", |
| 490 |
Float32: "float32", |
| 491 |
Float64: "float64", |
| 492 |
Complex64: "complex64", |
| 493 |
Complex128: "complex128", |
| 494 |
Array: "array", |
| 495 |
Chan: "chan", |
| 496 |
Func: "func", |
| 497 |
Interface: "interface", |
| 498 |
Map: "map", |
| 499 |
Pointer: "ptr", |
| 500 |
Slice: "slice", |
| 501 |
String: "string", |
| 502 |
Struct: "struct", |
| 503 |
UnsafePointer: "unsafe.Pointer", |
| 504 |
} |
| 505 |
|
| 506 |
// resolveNameOff resolves a name offset from a base pointer. |
| 507 |
// The (*rtype).nameOff method is a convenience wrapper for this function. |
| 508 |
// Implemented in the runtime package. |
| 509 |
// |
| 510 |
//go:noescape |
| 511 |
func resolveNameOff(ptrInModule unsafe.Pointer, off int32) unsafe.Pointer |
| 512 |
|
| 513 |
// resolveTypeOff resolves an *rtype offset from a base type. |
| 514 |
// The (*rtype).typeOff method is a convenience wrapper for this function. |
| 515 |
// Implemented in the runtime package. |
| 516 |
// |
| 517 |
//go:noescape |
| 518 |
func resolveTypeOff(rtype unsafe.Pointer, off int32) unsafe.Pointer |
| 519 |
|
| 520 |
// resolveTextOff resolves a function pointer offset from a base type. |
| 521 |
// The (*rtype).textOff method is a convenience wrapper for this function. |
| 522 |
// Implemented in the runtime package. |
| 523 |
// |
| 524 |
//go:noescape |
| 525 |
func resolveTextOff(rtype unsafe.Pointer, off int32) unsafe.Pointer |
| 526 |
|
| 527 |
// addReflectOff adds a pointer to the reflection lookup map in the runtime. |
| 528 |
// It returns a new ID that can be used as a typeOff or textOff, and will |
| 529 |
// be resolved correctly. Implemented in the runtime package. |
| 530 |
// |
| 531 |
// addReflectOff should be an internal detail, |
| 532 |
// but widely used packages access it using linkname. |
| 533 |
// Notable members of the hall of shame include: |
| 534 |
// - github.com/goplus/reflectx |
| 535 |
// |
| 536 |
// Do not remove or change the type signature. |
| 537 |
// See go.dev/issue/67401. |
| 538 |
// |
| 539 |
//go:linkname addReflectOff |
| 540 |
//go:noescape |
| 541 |
func addReflectOff(ptr unsafe.Pointer) int32 |
| 542 |
|
| 543 |
// resolveReflectName adds a name to the reflection lookup map in the runtime. |
| 544 |
// It returns a new nameOff that can be used to refer to the pointer. |
| 545 |
func resolveReflectName(n abi.Name) aNameOff { |
| 546 |
return aNameOff(addReflectOff(unsafe.Pointer(n.Bytes))) |
| 547 |
} |
| 548 |
|
| 549 |
// resolveReflectType adds a *rtype to the reflection lookup map in the runtime. |
| 550 |
// It returns a new typeOff that can be used to refer to the pointer. |
| 551 |
func resolveReflectType(t *abi.Type) aTypeOff { |
| 552 |
return aTypeOff(addReflectOff(unsafe.Pointer(t))) |
| 553 |
} |
| 554 |
|
| 555 |
// resolveReflectText adds a function pointer to the reflection lookup map in |
| 556 |
// the runtime. It returns a new textOff that can be used to refer to the |
| 557 |
// pointer. |
| 558 |
func resolveReflectText(ptr unsafe.Pointer) aTextOff { |
| 559 |
return aTextOff(addReflectOff(ptr)) |
| 560 |
} |
| 561 |
|
| 562 |
func (t *rtype) nameOff(off aNameOff) abi.Name { |
| 563 |
return abi.Name{Bytes: (*byte)(resolveNameOff(unsafe.Pointer(t), int32(off)))} |
| 564 |
} |
| 565 |
|
| 566 |
func (t *rtype) typeOff(off aTypeOff) *abi.Type { |
| 567 |
return (*abi.Type)(resolveTypeOff(unsafe.Pointer(t), int32(off))) |
| 568 |
} |
| 569 |
|
| 570 |
func (t *rtype) textOff(off aTextOff) unsafe.Pointer { |
| 571 |
return resolveTextOff(unsafe.Pointer(t), int32(off)) |
| 572 |
} |
| 573 |
|
| 574 |
func textOffFor(t *abi.Type, off aTextOff) unsafe.Pointer { |
| 575 |
return toRType(t).textOff(off) |
| 576 |
} |
| 577 |
|
| 578 |
func (t *rtype) String() string { |
| 579 |
s := t.nameOff(t.t.Str).Name() |
| 580 |
if t.t.TFlag&abi.TFlagExtraStar != 0 { |
| 581 |
return s[1:] |
| 582 |
} |
| 583 |
return s |
| 584 |
} |
| 585 |
|
| 586 |
func (t *rtype) Size() uintptr { return t.t.Size() } |
| 587 |
|
| 588 |
func (t *rtype) Bits() int { |
| 589 |
if t == nil { |
| 590 |
panic("reflect: Bits of nil Type") |
| 591 |
} |
| 592 |
k := t.Kind() |
| 593 |
if k < Int || k > Complex128 { |
| 594 |
panic("reflect: Bits of non-arithmetic Type " + t.String()) |
| 595 |
} |
| 596 |
return int(t.t.Size_) * 8 |
| 597 |
} |
| 598 |
|
| 599 |
func (t *rtype) Align() int { return t.t.Align() } |
| 600 |
|
| 601 |
func (t *rtype) FieldAlign() int { return t.t.FieldAlign() } |
| 602 |
|
| 603 |
func (t *rtype) Kind() Kind { return Kind(t.t.Kind()) } |
| 604 |
|
| 605 |
func (t *rtype) exportedMethods() []abi.Method { |
| 606 |
ut := t.uncommon() |
| 607 |
if ut == nil { |
| 608 |
return nil |
| 609 |
} |
| 610 |
return ut.ExportedMethods() |
| 611 |
} |
| 612 |
|
| 613 |
func (t *rtype) NumMethod() int { |
| 614 |
if t.Kind() == Interface { |
| 615 |
tt := (*interfaceType)(unsafe.Pointer(t)) |
| 616 |
return tt.NumMethod() |
| 617 |
} |
| 618 |
return len(t.exportedMethods()) |
| 619 |
} |
| 620 |
|
| 621 |
func (t *rtype) Method(i int) (m Method) { |
| 622 |
if t.Kind() == Interface { |
| 623 |
tt := (*interfaceType)(unsafe.Pointer(t)) |
| 624 |
return tt.Method(i) |
| 625 |
} |
| 626 |
methods := t.exportedMethods() |
| 627 |
if i < 0 || i >= len(methods) { |
| 628 |
panic("reflect: Method index out of range") |
| 629 |
} |
| 630 |
p := methods[i] |
| 631 |
pname := t.nameOff(p.Name) |
| 632 |
m.Name = pname.Name() |
| 633 |
fl := flag(Func) |
| 634 |
mtyp := t.typeOff(p.Mtyp) |
| 635 |
ft := (*funcType)(unsafe.Pointer(mtyp)) |
| 636 |
in := make([]Type, 0, 1+ft.NumIn()) |
| 637 |
in = append(in, t) |
| 638 |
for _, arg := range ft.InSlice() { |
| 639 |
in = append(in, toRType(arg)) |
| 640 |
} |
| 641 |
out := make([]Type, 0, ft.NumOut()) |
| 642 |
for _, ret := range ft.OutSlice() { |
| 643 |
out = append(out, toRType(ret)) |
| 644 |
} |
| 645 |
mt := FuncOf(in, out, ft.IsVariadic()) |
| 646 |
m.Type = mt |
| 647 |
tfn := t.textOff(p.Tfn) |
| 648 |
fn := unsafe.Pointer(&tfn) |
| 649 |
m.Func = Value{&mt.(*rtype).t, fn, fl} |
| 650 |
|
| 651 |
m.Index = i |
| 652 |
return m |
| 653 |
} |
| 654 |
|
| 655 |
func (t *rtype) MethodByName(name string) (m Method, ok bool) { |
| 656 |
if t.Kind() == Interface { |
| 657 |
tt := (*interfaceType)(unsafe.Pointer(t)) |
| 658 |
return tt.MethodByName(name) |
| 659 |
} |
| 660 |
ut := t.uncommon() |
| 661 |
if ut == nil { |
| 662 |
return Method{}, false |
| 663 |
} |
| 664 |
|
| 665 |
methods := ut.ExportedMethods() |
| 666 |
|
| 667 |
// We are looking for the first index i where the string becomes >= s. |
| 668 |
// This is a copy of sort.Search, with f(h) replaced by (t.nameOff(methods[h].name).name() >= name). |
| 669 |
i, j := 0, len(methods) |
| 670 |
for i < j { |
| 671 |
h := int(uint(i+j) >> 1) // avoid overflow when computing h |
| 672 |
// i ≤ h < j |
| 673 |
if !(t.nameOff(methods[h].Name).Name() >= name) { |
| 674 |
i = h + 1 // preserves f(i-1) == false |
| 675 |
} else { |
| 676 |
j = h // preserves f(j) == true |
| 677 |
} |
| 678 |
} |
| 679 |
// i == j, f(i-1) == false, and f(j) (= f(i)) == true => answer is i. |
| 680 |
if i < len(methods) && name == t.nameOff(methods[i].Name).Name() { |
| 681 |
return t.Method(i), true |
| 682 |
} |
| 683 |
|
| 684 |
return Method{}, false |
| 685 |
} |
| 686 |
|
| 687 |
func (t *rtype) PkgPath() string { |
| 688 |
if t.t.TFlag&abi.TFlagNamed == 0 { |
| 689 |
return "" |
| 690 |
} |
| 691 |
ut := t.uncommon() |
| 692 |
if ut == nil { |
| 693 |
return "" |
| 694 |
} |
| 695 |
return t.nameOff(ut.PkgPath).Name() |
| 696 |
} |
| 697 |
|
| 698 |
func pkgPathFor(t *abi.Type) string { |
| 699 |
return toRType(t).PkgPath() |
| 700 |
} |
| 701 |
|
| 702 |
func (t *rtype) Name() string { |
| 703 |
if !t.t.HasName() { |
| 704 |
return "" |
| 705 |
} |
| 706 |
s := t.String() |
| 707 |
i := len(s) - 1 |
| 708 |
sqBrackets := 0 |
| 709 |
for i >= 0 && (s[i] != '.' || sqBrackets != 0) { |
| 710 |
switch s[i] { |
| 711 |
case ']': |
| 712 |
sqBrackets++ |
| 713 |
case '[': |
| 714 |
sqBrackets-- |
| 715 |
} |
| 716 |
i-- |
| 717 |
} |
| 718 |
return s[i+1:] |
| 719 |
} |
| 720 |
|
| 721 |
func nameFor(t *abi.Type) string { |
| 722 |
return toRType(t).Name() |
| 723 |
} |
| 724 |
|
| 725 |
func (t *rtype) ChanDir() ChanDir { |
| 726 |
if t.Kind() != Chan { |
| 727 |
panic("reflect: ChanDir of non-chan type " + t.String()) |
| 728 |
} |
| 729 |
tt := (*abi.ChanType)(unsafe.Pointer(t)) |
| 730 |
return ChanDir(tt.Dir) |
| 731 |
} |
| 732 |
|
| 733 |
func toRType(t *abi.Type) *rtype { |
| 734 |
return (*rtype)(unsafe.Pointer(t)) |
| 735 |
} |
| 736 |
|
| 737 |
func elem(t *abi.Type) *abi.Type { |
| 738 |
et := t.Elem() |
| 739 |
if et != nil { |
| 740 |
return et |
| 741 |
} |
| 742 |
panic("reflect: Elem of invalid type " + stringFor(t)) |
| 743 |
} |
| 744 |
|
| 745 |
func (t *rtype) Elem() Type { |
| 746 |
return toType(elem(t.common())) |
| 747 |
} |
| 748 |
|
| 749 |
func (t *rtype) Field(i int) StructField { |
| 750 |
if t.Kind() != Struct { |
| 751 |
panic("reflect: Field of non-struct type " + t.String()) |
| 752 |
} |
| 753 |
tt := (*structType)(unsafe.Pointer(t)) |
| 754 |
return tt.Field(i) |
| 755 |
} |
| 756 |
|
| 757 |
func (t *rtype) FieldByIndex(index []int) StructField { |
| 758 |
if t.Kind() != Struct { |
| 759 |
panic("reflect: FieldByIndex of non-struct type " + t.String()) |
| 760 |
} |
| 761 |
tt := (*structType)(unsafe.Pointer(t)) |
| 762 |
return tt.FieldByIndex(index) |
| 763 |
} |
| 764 |
|
| 765 |
func (t *rtype) FieldByName(name string) (StructField, bool) { |
| 766 |
if t.Kind() != Struct { |
| 767 |
panic("reflect: FieldByName of non-struct type " + t.String()) |
| 768 |
} |
| 769 |
tt := (*structType)(unsafe.Pointer(t)) |
| 770 |
return tt.FieldByName(name) |
| 771 |
} |
| 772 |
|
| 773 |
func (t *rtype) FieldByNameFunc(match func(string) bool) (StructField, bool) { |
| 774 |
if t.Kind() != Struct { |
| 775 |
panic("reflect: FieldByNameFunc of non-struct type " + t.String()) |
| 776 |
} |
| 777 |
tt := (*structType)(unsafe.Pointer(t)) |
| 778 |
return tt.FieldByNameFunc(match) |
| 779 |
} |
| 780 |
|
| 781 |
func (t *rtype) Len() int { |
| 782 |
if t.Kind() != Array { |
| 783 |
panic("reflect: Len of non-array type " + t.String()) |
| 784 |
} |
| 785 |
tt := (*arrayType)(unsafe.Pointer(t)) |
| 786 |
return int(tt.Len) |
| 787 |
} |
| 788 |
|
| 789 |
func (t *rtype) NumField() int { |
| 790 |
if t.Kind() != Struct { |
| 791 |
panic("reflect: NumField of non-struct type " + t.String()) |
| 792 |
} |
| 793 |
tt := (*structType)(unsafe.Pointer(t)) |
| 794 |
return len(tt.Fields) |
| 795 |
} |
| 796 |
|
| 797 |
func (t *rtype) In(i int) Type { |
| 798 |
if t.Kind() != Func { |
| 799 |
panic("reflect: In of non-func type " + t.String()) |
| 800 |
} |
| 801 |
tt := (*abi.FuncType)(unsafe.Pointer(t)) |
| 802 |
return toType(tt.InSlice()[i]) |
| 803 |
} |
| 804 |
|
| 805 |
func (t *rtype) NumIn() int { |
| 806 |
if t.Kind() != Func { |
| 807 |
panic("reflect: NumIn of non-func type " + t.String()) |
| 808 |
} |
| 809 |
tt := (*abi.FuncType)(unsafe.Pointer(t)) |
| 810 |
return tt.NumIn() |
| 811 |
} |
| 812 |
|
| 813 |
func (t *rtype) NumOut() int { |
| 814 |
if t.Kind() != Func { |
| 815 |
panic("reflect: NumOut of non-func type " + t.String()) |
| 816 |
} |
| 817 |
tt := (*abi.FuncType)(unsafe.Pointer(t)) |
| 818 |
return tt.NumOut() |
| 819 |
} |
| 820 |
|
| 821 |
func (t *rtype) Out(i int) Type { |
| 822 |
if t.Kind() != Func { |
| 823 |
panic("reflect: Out of non-func type " + t.String()) |
| 824 |
} |
| 825 |
tt := (*abi.FuncType)(unsafe.Pointer(t)) |
| 826 |
return toType(tt.OutSlice()[i]) |
| 827 |
} |
| 828 |
|
| 829 |
func (t *rtype) IsVariadic() bool { |
| 830 |
if t.Kind() != Func { |
| 831 |
panic("reflect: IsVariadic of non-func type " + t.String()) |
| 832 |
} |
| 833 |
tt := (*abi.FuncType)(unsafe.Pointer(t)) |
| 834 |
return tt.IsVariadic() |
| 835 |
} |
| 836 |
|
| 837 |
func (t *rtype) OverflowComplex(x complex128) bool { |
| 838 |
k := t.Kind() |
| 839 |
switch k { |
| 840 |
case Complex64: |
| 841 |
return overflowFloat32(real(x)) || overflowFloat32(imag(x)) |
| 842 |
case Complex128: |
| 843 |
return false |
| 844 |
} |
| 845 |
panic("reflect: OverflowComplex of non-complex type " + t.String()) |
| 846 |
} |
| 847 |
|
| 848 |
func (t *rtype) OverflowFloat(x float64) bool { |
| 849 |
k := t.Kind() |
| 850 |
switch k { |
| 851 |
case Float32: |
| 852 |
return overflowFloat32(x) |
| 853 |
case Float64: |
| 854 |
return false |
| 855 |
} |
| 856 |
panic("reflect: OverflowFloat of non-float type " + t.String()) |
| 857 |
} |
| 858 |
|
| 859 |
func (t *rtype) OverflowInt(x int64) bool { |
| 860 |
k := t.Kind() |
| 861 |
switch k { |
| 862 |
case Int, Int8, Int16, Int32, Int64: |
| 863 |
bitSize := t.Size() * 8 |
| 864 |
trunc := (x << (64 - bitSize)) >> (64 - bitSize) |
| 865 |
return x != trunc |
| 866 |
} |
| 867 |
panic("reflect: OverflowInt of non-int type " + t.String()) |
| 868 |
} |
| 869 |
|
| 870 |
func (t *rtype) OverflowUint(x uint64) bool { |
| 871 |
k := t.Kind() |
| 872 |
switch k { |
| 873 |
case Uint, Uintptr, Uint8, Uint16, Uint32, Uint64: |
| 874 |
bitSize := t.Size() * 8 |
| 875 |
trunc := (x << (64 - bitSize)) >> (64 - bitSize) |
| 876 |
return x != trunc |
| 877 |
} |
| 878 |
panic("reflect: OverflowUint of non-uint type " + t.String()) |
| 879 |
} |
| 880 |
|
| 881 |
func (t *rtype) CanSeq() bool { |
| 882 |
switch t.Kind() { |
| 883 |
case Int8, Int16, Int32, Int64, Int, Uint8, Uint16, Uint32, Uint64, Uint, Uintptr, Array, Slice, Chan, String, Map: |
| 884 |
return true |
| 885 |
case Func: |
| 886 |
return canRangeFunc(&t.t) |
| 887 |
case Pointer: |
| 888 |
return t.Elem().Kind() == Array |
| 889 |
} |
| 890 |
return false |
| 891 |
} |
| 892 |
|
| 893 |
func canRangeFunc(t *abi.Type) bool { |
| 894 |
if t.Kind() != abi.Func { |
| 895 |
return false |
| 896 |
} |
| 897 |
f := t.FuncType() |
| 898 |
if f.InCount != 1 || f.OutCount != 0 { |
| 899 |
return false |
| 900 |
} |
| 901 |
y := f.In(0) |
| 902 |
if y.Kind() != abi.Func { |
| 903 |
return false |
| 904 |
} |
| 905 |
yield := y.FuncType() |
| 906 |
return yield.InCount == 1 && yield.OutCount == 1 && yield.Out(0).Kind() == abi.Bool |
| 907 |
} |
| 908 |
|
| 909 |
func (t *rtype) CanSeq2() bool { |
| 910 |
switch t.Kind() { |
| 911 |
case Array, Slice, String, Map: |
| 912 |
return true |
| 913 |
case Func: |
| 914 |
return canRangeFunc2(&t.t) |
| 915 |
case Pointer: |
| 916 |
return t.Elem().Kind() == Array |
| 917 |
} |
| 918 |
return false |
| 919 |
} |
| 920 |
|
| 921 |
func canRangeFunc2(t *abi.Type) bool { |
| 922 |
if t.Kind() != abi.Func { |
| 923 |
return false |
| 924 |
} |
| 925 |
f := t.FuncType() |
| 926 |
if f.InCount != 1 || f.OutCount != 0 { |
| 927 |
return false |
| 928 |
} |
| 929 |
y := f.In(0) |
| 930 |
if y.Kind() != abi.Func { |
| 931 |
return false |
| 932 |
} |
| 933 |
yield := y.FuncType() |
| 934 |
return yield.InCount == 2 && yield.OutCount == 1 && yield.Out(0).Kind() == abi.Bool |
| 935 |
} |
| 936 |
|
| 937 |
// add returns p+x. |
| 938 |
// |
| 939 |
// The whySafe string is ignored, so that the function still inlines |
| 940 |
// as efficiently as p+x, but all call sites should use the string to |
| 941 |
// record why the addition is safe, which is to say why the addition |
| 942 |
// does not cause x to advance to the very end of p's allocation |
| 943 |
// and therefore point incorrectly at the next block in memory. |
| 944 |
// |
| 945 |
// add should be an internal detail (and is trivially copyable), |
| 946 |
// but widely used packages access it using linkname. |
| 947 |
// Notable members of the hall of shame include: |
| 948 |
// - github.com/pinpoint-apm/pinpoint-go-agent |
| 949 |
// - github.com/vmware/govmomi |
| 950 |
// |
| 951 |
// Do not remove or change the type signature. |
| 952 |
// See go.dev/issue/67401. |
| 953 |
// |
| 954 |
//go:linkname add |
| 955 |
func add(p unsafe.Pointer, x uintptr, whySafe string) unsafe.Pointer { |
| 956 |
return unsafe.Pointer(uintptr(p) + x) |
| 957 |
} |
| 958 |
|
| 959 |
func (d ChanDir) String() string { |
| 960 |
switch d { |
| 961 |
case SendDir: |
| 962 |
return "chan<-" |
| 963 |
case RecvDir: |
| 964 |
return "<-chan" |
| 965 |
case BothDir: |
| 966 |
return "chan" |
| 967 |
} |
| 968 |
return "ChanDir" + strconv.Itoa(int(d)) |
| 969 |
} |
| 970 |
|
| 971 |
// Method returns the i'th method in the type's method set. |
| 972 |
func (t *interfaceType) Method(i int) (m Method) { |
| 973 |
if i < 0 || i >= len(t.Methods) { |
| 974 |
return |
| 975 |
} |
| 976 |
p := &t.Methods[i] |
| 977 |
pname := t.nameOff(p.Name) |
| 978 |
m.Name = pname.Name() |
| 979 |
if !pname.IsExported() { |
| 980 |
m.PkgPath = pkgPath(pname) |
| 981 |
if m.PkgPath == "" { |
| 982 |
m.PkgPath = t.PkgPath.Name() |
| 983 |
} |
| 984 |
} |
| 985 |
m.Type = toType(t.typeOff(p.Typ)) |
| 986 |
m.Index = i |
| 987 |
return |
| 988 |
} |
| 989 |
|
| 990 |
// NumMethod returns the number of interface methods in the type's method set. |
| 991 |
func (t *interfaceType) NumMethod() int { return len(t.Methods) } |
| 992 |
|
| 993 |
// MethodByName method with the given name in the type's method set. |
| 994 |
func (t *interfaceType) MethodByName(name string) (m Method, ok bool) { |
| 995 |
if t == nil { |
| 996 |
return |
| 997 |
} |
| 998 |
var p *abi.Imethod |
| 999 |
for i := range t.Methods { |
| 1000 |
p = &t.Methods[i] |
| 1001 |
if t.nameOff(p.Name).Name() == name { |
| 1002 |
return t.Method(i), true |
| 1003 |
} |
| 1004 |
} |
| 1005 |
return |
| 1006 |
} |
| 1007 |
|
| 1008 |
// A StructField describes a single field in a struct. |
| 1009 |
type StructField struct { |
| 1010 |
// Name is the field name. |
| 1011 |
Name string |
| 1012 |
|
| 1013 |
// PkgPath is the package path that qualifies a lower case (unexported) |
| 1014 |
// field name. It is empty for upper case (exported) field names. |
| 1015 |
// See https://golang.org/ref/spec#Uniqueness_of_identifiers |
| 1016 |
PkgPath string |
| 1017 |
|
| 1018 |
Type Type // field type |
| 1019 |
Tag StructTag // field tag string |
| 1020 |
Offset uintptr // offset within struct, in bytes |
| 1021 |
Index []int // index sequence for Type.FieldByIndex |
| 1022 |
Anonymous bool // is an embedded field |
| 1023 |
} |
| 1024 |
|
| 1025 |
// IsExported reports whether the field is exported. |
| 1026 |
func (f StructField) IsExported() bool { |
| 1027 |
return f.PkgPath == "" |
| 1028 |
} |
| 1029 |
|
| 1030 |
// A StructTag is the tag string in a struct field. |
| 1031 |
// |
| 1032 |
// By convention, tag strings are a concatenation of |
| 1033 |
// optionally space-separated key:"value" pairs. |
| 1034 |
// Each key is a non-empty string consisting of non-control |
| 1035 |
// characters other than space (U+0020 ' '), quote (U+0022 '"'), |
| 1036 |
// and colon (U+003A ':'). Each value is quoted using U+0022 '"' |
| 1037 |
// characters and Go string literal syntax. |
| 1038 |
type StructTag string |
| 1039 |
|
| 1040 |
// Get returns the value associated with key in the tag string. |
| 1041 |
// If there is no such key in the tag, Get returns the empty string. |
| 1042 |
// If the tag does not have the conventional format, the value |
| 1043 |
// returned by Get is unspecified. To determine whether a tag is |
| 1044 |
// explicitly set to the empty string, use [StructTag.Lookup]. |
| 1045 |
func (tag StructTag) Get(key string) string { |
| 1046 |
v, _ := tag.Lookup(key) |
| 1047 |
return v |
| 1048 |
} |
| 1049 |
|
| 1050 |
// Lookup returns the value associated with key in the tag string. |
| 1051 |
// If the key is present in the tag the value (which may be empty) |
| 1052 |
// is returned. Otherwise the returned value will be the empty string. |
| 1053 |
// The ok return value reports whether the value was explicitly set in |
| 1054 |
// the tag string. If the tag does not have the conventional format, |
| 1055 |
// the value returned by Lookup is unspecified. |
| 1056 |
func (tag StructTag) Lookup(key string) (value string, ok bool) { |
| 1057 |
// When modifying this code, also update the validateStructTag code |
| 1058 |
// in cmd/vet/structtag.go. |
| 1059 |
|
| 1060 |
for tag != "" { |
| 1061 |
// Skip leading space. |
| 1062 |
i := 0 |
| 1063 |
for i < len(tag) && tag[i] == ' ' { |
| 1064 |
i++ |
| 1065 |
} |
| 1066 |
tag = tag[i:] |
| 1067 |
if tag == "" { |
| 1068 |
break |
| 1069 |
} |
| 1070 |
|
| 1071 |
// Scan to colon. A space, a quote or a control character is a syntax error. |
| 1072 |
// Strictly speaking, control chars include the range [0x7f, 0x9f], not just |
| 1073 |
// [0x00, 0x1f], but in practice, we ignore the multi-byte control characters |
| 1074 |
// as it is simpler to inspect the tag's bytes than the tag's runes. |
| 1075 |
i = 0 |
| 1076 |
for i < len(tag) && tag[i] > ' ' && tag[i] != ':' && tag[i] != '"' && tag[i] != 0x7f { |
| 1077 |
i++ |
| 1078 |
} |
| 1079 |
if i == 0 || i+1 >= len(tag) || tag[i] != ':' || tag[i+1] != '"' { |
| 1080 |
break |
| 1081 |
} |
| 1082 |
name := string(tag[:i]) |
| 1083 |
tag = tag[i+1:] |
| 1084 |
|
| 1085 |
// Scan quoted string to find value. |
| 1086 |
i = 1 |
| 1087 |
for i < len(tag) && tag[i] != '"' { |
| 1088 |
if tag[i] == '\\' { |
| 1089 |
i++ |
| 1090 |
} |
| 1091 |
i++ |
| 1092 |
} |
| 1093 |
if i >= len(tag) { |
| 1094 |
break |
| 1095 |
} |
| 1096 |
qvalue := string(tag[:i+1]) |
| 1097 |
tag = tag[i+1:] |
| 1098 |
|
| 1099 |
if key == name { |
| 1100 |
value, err := strconv.Unquote(qvalue) |
| 1101 |
if err != nil { |
| 1102 |
break |
| 1103 |
} |
| 1104 |
return value, true |
| 1105 |
} |
| 1106 |
} |
| 1107 |
return "", false |
| 1108 |
} |
| 1109 |
|
| 1110 |
// Field returns the i'th struct field. |
| 1111 |
func (t *structType) Field(i int) (f StructField) { |
| 1112 |
if i < 0 || i >= len(t.Fields) { |
| 1113 |
panic("reflect: Field index out of bounds") |
| 1114 |
} |
| 1115 |
p := &t.Fields[i] |
| 1116 |
f.Type = toType(p.Typ) |
| 1117 |
f.Name = p.Name.Name() |
| 1118 |
f.Anonymous = p.Embedded() |
| 1119 |
if !p.Name.IsExported() { |
| 1120 |
f.PkgPath = t.PkgPath.Name() |
| 1121 |
} |
| 1122 |
if tag := p.Name.Tag(); tag != "" { |
| 1123 |
f.Tag = StructTag(tag) |
| 1124 |
} |
| 1125 |
f.Offset = p.Offset |
| 1126 |
|
| 1127 |
// We can't safely use this optimization on js or wasi, |
| 1128 |
// which do not appear to support read-only data. |
| 1129 |
if i < 256 && runtime.GOOS != "js" && runtime.GOOS != "wasip1" { |
| 1130 |
staticuint64s := getStaticuint64s() |
| 1131 |
p := unsafe.Pointer(&(*staticuint64s)[i]) |
| 1132 |
if unsafe.Sizeof(int(0)) == 4 && goarch.BigEndian { |
| 1133 |
p = unsafe.Add(p, 4) |
| 1134 |
} |
| 1135 |
f.Index = unsafe.Slice((*int)(p), 1) |
| 1136 |
} else { |
| 1137 |
// NOTE(rsc): This is the only allocation in the interface |
| 1138 |
// presented by a reflect.Type. It would be nice to avoid, |
| 1139 |
// but we need to make sure that misbehaving clients of |
| 1140 |
// reflect cannot affect other uses of reflect. |
| 1141 |
// One possibility is CL 5371098, but we postponed that |
| 1142 |
// ugliness until there is a demonstrated |
| 1143 |
// need for the performance. This is issue 2320. |
| 1144 |
f.Index = []int{i} |
| 1145 |
} |
| 1146 |
return |
| 1147 |
} |
| 1148 |
|
| 1149 |
// getStaticuint64s returns a pointer to an array of 256 uint64 values, |
| 1150 |
// defined in the runtime package in read-only memory. |
| 1151 |
// staticuint64s[0] == 0, staticuint64s[1] == 1, and so forth. |
| 1152 |
// |
| 1153 |
//go:linkname getStaticuint64s runtime.getStaticuint64s |
| 1154 |
func getStaticuint64s() *[256]uint64 |
| 1155 |
|
| 1156 |
// TODO(gri): Should there be an error/bool indicator if the index |
| 1157 |
// is wrong for FieldByIndex? |
| 1158 |
|
| 1159 |
// FieldByIndex returns the nested field corresponding to index. |
| 1160 |
func (t *structType) FieldByIndex(index []int) (f StructField) { |
| 1161 |
f.Type = toType(&t.Type) |
| 1162 |
for i, x := range index { |
| 1163 |
if i > 0 { |
| 1164 |
ft := f.Type |
| 1165 |
if ft.Kind() == Pointer && ft.Elem().Kind() == Struct { |
| 1166 |
ft = ft.Elem() |
| 1167 |
} |
| 1168 |
f.Type = ft |
| 1169 |
} |
| 1170 |
f = f.Type.Field(x) |
| 1171 |
} |
| 1172 |
return |
| 1173 |
} |
| 1174 |
|
| 1175 |
// A fieldScan represents an item on the fieldByNameFunc scan work list. |
| 1176 |
type fieldScan struct { |
| 1177 |
typ *structType |
| 1178 |
index []int |
| 1179 |
} |
| 1180 |
|
| 1181 |
// FieldByNameFunc returns the struct field with a name that satisfies the |
| 1182 |
// match function and a boolean to indicate if the field was found. |
| 1183 |
func (t *structType) FieldByNameFunc(match func(string) bool) (result StructField, ok bool) { |
| 1184 |
// This uses the same condition that the Go language does: there must be a unique instance |
| 1185 |
// of the match at a given depth level. If there are multiple instances of a match at the |
| 1186 |
// same depth, they annihilate each other and inhibit any possible match at a lower level. |
| 1187 |
// The algorithm is breadth first search, one depth level at a time. |
| 1188 |
|
| 1189 |
// The current and next slices are work queues: |
| 1190 |
// current lists the fields to visit on this depth level, |
| 1191 |
// and next lists the fields on the next lower level. |
| 1192 |
current := []fieldScan{} |
| 1193 |
next := []fieldScan{{typ: t}} |
| 1194 |
|
| 1195 |
// nextCount records the number of times an embedded type has been |
| 1196 |
// encountered and considered for queueing in the 'next' slice. |
| 1197 |
// We only queue the first one, but we increment the count on each. |
| 1198 |
// If a struct type T can be reached more than once at a given depth level, |
| 1199 |
// then it annihilates itself and need not be considered at all when we |
| 1200 |
// process that next depth level. |
| 1201 |
var nextCount map[*structType]int |
| 1202 |
|
| 1203 |
// visited records the structs that have been considered already. |
| 1204 |
// Embedded pointer fields can create cycles in the graph of |
| 1205 |
// reachable embedded types; visited avoids following those cycles. |
| 1206 |
// It also avoids duplicated effort: if we didn't find the field in an |
| 1207 |
// embedded type T at level 2, we won't find it in one at level 4 either. |
| 1208 |
visited := map[*structType]bool{} |
| 1209 |
|
| 1210 |
for len(next) > 0 { |
| 1211 |
current, next = next, current[:0] |
| 1212 |
count := nextCount |
| 1213 |
nextCount = nil |
| 1214 |
|
| 1215 |
// Process all the fields at this depth, now listed in 'current'. |
| 1216 |
// The loop queues embedded fields found in 'next', for processing during the next |
| 1217 |
// iteration. The multiplicity of the 'current' field counts is recorded |
| 1218 |
// in 'count'; the multiplicity of the 'next' field counts is recorded in 'nextCount'. |
| 1219 |
for _, scan := range current { |
| 1220 |
t := scan.typ |
| 1221 |
if visited[t] { |
| 1222 |
// We've looked through this type before, at a higher level. |
| 1223 |
// That higher level would shadow the lower level we're now at, |
| 1224 |
// so this one can't be useful to us. Ignore it. |
| 1225 |
continue |
| 1226 |
} |
| 1227 |
visited[t] = true |
| 1228 |
for i := range t.Fields { |
| 1229 |
f := &t.Fields[i] |
| 1230 |
// Find name and (for embedded field) type for field f. |
| 1231 |
fname := f.Name.Name() |
| 1232 |
var ntyp *abi.Type |
| 1233 |
if f.Embedded() { |
| 1234 |
// Embedded field of type T or *T. |
| 1235 |
ntyp = f.Typ |
| 1236 |
if ntyp.Kind() == abi.Pointer { |
| 1237 |
ntyp = ntyp.Elem() |
| 1238 |
} |
| 1239 |
} |
| 1240 |
|
| 1241 |
// Does it match? |
| 1242 |
if match(fname) { |
| 1243 |
// Potential match |
| 1244 |
if count[t] > 1 || ok { |
| 1245 |
// Name appeared multiple times at this level: annihilate. |
| 1246 |
return StructField{}, false |
| 1247 |
} |
| 1248 |
result = t.Field(i) |
| 1249 |
result.Index = nil |
| 1250 |
result.Index = append(result.Index, scan.index...) |
| 1251 |
result.Index = append(result.Index, i) |
| 1252 |
ok = true |
| 1253 |
continue |
| 1254 |
} |
| 1255 |
|
| 1256 |
// Queue embedded struct fields for processing with next level, |
| 1257 |
// but only if we haven't seen a match yet at this level and only |
| 1258 |
// if the embedded types haven't already been queued. |
| 1259 |
if ok || ntyp == nil || ntyp.Kind() != abi.Struct { |
| 1260 |
continue |
| 1261 |
} |
| 1262 |
styp := (*structType)(unsafe.Pointer(ntyp)) |
| 1263 |
if nextCount[styp] > 0 { |
| 1264 |
nextCount[styp] = 2 // exact multiple doesn't matter |
| 1265 |
continue |
| 1266 |
} |
| 1267 |
if nextCount == nil { |
| 1268 |
nextCount = map[*structType]int{} |
| 1269 |
} |
| 1270 |
nextCount[styp] = 1 |
| 1271 |
if count[t] > 1 { |
| 1272 |
nextCount[styp] = 2 // exact multiple doesn't matter |
| 1273 |
} |
| 1274 |
var index []int |
| 1275 |
index = append(index, scan.index...) |
| 1276 |
index = append(index, i) |
| 1277 |
next = append(next, fieldScan{styp, index}) |
| 1278 |
} |
| 1279 |
} |
| 1280 |
if ok { |
| 1281 |
break |
| 1282 |
} |
| 1283 |
} |
| 1284 |
return |
| 1285 |
} |
| 1286 |
|
| 1287 |
// FieldByName returns the struct field with the given name |
| 1288 |
// and a boolean to indicate if the field was found. |
| 1289 |
func (t *structType) FieldByName(name string) (f StructField, present bool) { |
| 1290 |
// Quick check for top-level name, or struct without embedded fields. |
| 1291 |
hasEmbeds := false |
| 1292 |
if name != "" { |
| 1293 |
for i := range t.Fields { |
| 1294 |
tf := &t.Fields[i] |
| 1295 |
if tf.Name.Name() == name { |
| 1296 |
return t.Field(i), true |
| 1297 |
} |
| 1298 |
if tf.Embedded() { |
| 1299 |
hasEmbeds = true |
| 1300 |
} |
| 1301 |
} |
| 1302 |
} |
| 1303 |
if !hasEmbeds { |
| 1304 |
return |
| 1305 |
} |
| 1306 |
return t.FieldByNameFunc(func(s string) bool { return s == name }) |
| 1307 |
} |
| 1308 |
|
| 1309 |
// TypeOf returns the reflection [Type] that represents the dynamic type of i. |
| 1310 |
// If i is a nil interface value, TypeOf returns nil. |
| 1311 |
func TypeOf(i any) Type { |
| 1312 |
return toType(abi.TypeOf(i)) |
| 1313 |
} |
| 1314 |
|
| 1315 |
// TypeFor returns the [Type] that represents the type argument T. |
| 1316 |
func TypeFor[T any]() Type { |
| 1317 |
return toType(abi.TypeFor[T]()) |
| 1318 |
} |
| 1319 |
|
| 1320 |
// rtypeOf directly extracts the *rtype of the provided value. |
| 1321 |
func rtypeOf(i any) *abi.Type { |
| 1322 |
return abi.TypeOf(i) |
| 1323 |
} |
| 1324 |
|
| 1325 |
// ptrMap is the cache for PointerTo. |
| 1326 |
var ptrMap sync.Map // map[*rtype]*ptrType |
| 1327 |
|
| 1328 |
// PtrTo returns the pointer type with element t. |
| 1329 |
// For example, if t represents type Foo, PtrTo(t) represents *Foo. |
| 1330 |
// |
| 1331 |
// PtrTo is the old spelling of [PointerTo]. |
| 1332 |
// The two functions behave identically. |
| 1333 |
// |
| 1334 |
// Deprecated: Superseded by [PointerTo]. |
| 1335 |
// |
| 1336 |
//go:fix inline |
| 1337 |
func PtrTo(t Type) Type { return PointerTo(t) } |
| 1338 |
|
| 1339 |
// PointerTo returns the pointer type with element t. |
| 1340 |
// For example, if t represents type Foo, PointerTo(t) represents *Foo. |
| 1341 |
func PointerTo(t Type) Type { |
| 1342 |
return toRType(t.(*rtype).ptrTo()) |
| 1343 |
} |
| 1344 |
|
| 1345 |
func (t *rtype) ptrTo() *abi.Type { |
| 1346 |
at := &t.t |
| 1347 |
if at.PtrToThis != 0 { |
| 1348 |
return t.typeOff(at.PtrToThis) |
| 1349 |
} |
| 1350 |
|
| 1351 |
// Check the cache. |
| 1352 |
if pi, ok := ptrMap.Load(t); ok { |
| 1353 |
return &pi.(*ptrType).Type |
| 1354 |
} |
| 1355 |
|
| 1356 |
// Look in known types. |
| 1357 |
s := "*" + t.String() |
| 1358 |
for _, tt := range typesByString(s) { |
| 1359 |
p := (*ptrType)(unsafe.Pointer(tt)) |
| 1360 |
if p.Elem != &t.t { |
| 1361 |
continue |
| 1362 |
} |
| 1363 |
pi, _ := ptrMap.LoadOrStore(t, p) |
| 1364 |
return &pi.(*ptrType).Type |
| 1365 |
} |
| 1366 |
|
| 1367 |
// Create a new ptrType starting with the description |
| 1368 |
// of an *unsafe.Pointer. |
| 1369 |
var iptr any = (*unsafe.Pointer)(nil) |
| 1370 |
prototype := *(**ptrType)(unsafe.Pointer(&iptr)) |
| 1371 |
pp := *prototype |
| 1372 |
|
| 1373 |
pp.Str = resolveReflectName(newName(s, "", false, false)) |
| 1374 |
pp.PtrToThis = 0 |
| 1375 |
|
| 1376 |
// For the type structures linked into the binary, the |
| 1377 |
// compiler provides a good hash of the string. |
| 1378 |
// Create a good hash for the new string by using |
| 1379 |
// the FNV-1 hash's mixing function to combine the |
| 1380 |
// old hash and the new "*". |
| 1381 |
pp.Hash = fnv1(t.t.Hash, '*') |
| 1382 |
|
| 1383 |
pp.Elem = at |
| 1384 |
|
| 1385 |
pi, _ := ptrMap.LoadOrStore(t, &pp) |
| 1386 |
return &pi.(*ptrType).Type |
| 1387 |
} |
| 1388 |
|
| 1389 |
func ptrTo(t *abi.Type) *abi.Type { |
| 1390 |
return toRType(t).ptrTo() |
| 1391 |
} |
| 1392 |
|
| 1393 |
// fnv1 incorporates the list of bytes into the hash x using the FNV-1 hash function. |
| 1394 |
func fnv1(x uint32, list ...byte) uint32 { |
| 1395 |
for _, b := range list { |
| 1396 |
x = x*16777619 ^ uint32(b) |
| 1397 |
} |
| 1398 |
return x |
| 1399 |
} |
| 1400 |
|
| 1401 |
func (t *rtype) Implements(u Type) bool { |
| 1402 |
if u == nil { |
| 1403 |
panic("reflect: nil type passed to Type.Implements") |
| 1404 |
} |
| 1405 |
if u.Kind() != Interface { |
| 1406 |
panic("reflect: non-interface type passed to Type.Implements") |
| 1407 |
} |
| 1408 |
return implements(u.common(), t.common()) |
| 1409 |
} |
| 1410 |
|
| 1411 |
func (t *rtype) AssignableTo(u Type) bool { |
| 1412 |
if u == nil { |
| 1413 |
panic("reflect: nil type passed to Type.AssignableTo") |
| 1414 |
} |
| 1415 |
uu := u.common() |
| 1416 |
return directlyAssignable(uu, t.common()) || implements(uu, t.common()) |
| 1417 |
} |
| 1418 |
|
| 1419 |
func (t *rtype) ConvertibleTo(u Type) bool { |
| 1420 |
if u == nil { |
| 1421 |
panic("reflect: nil type passed to Type.ConvertibleTo") |
| 1422 |
} |
| 1423 |
return convertOp(u.common(), t.common()) != nil |
| 1424 |
} |
| 1425 |
|
| 1426 |
func (t *rtype) Comparable() bool { |
| 1427 |
return t.t.Equal != nil |
| 1428 |
} |
| 1429 |
|
| 1430 |
// implements reports whether the type V implements the interface type T. |
| 1431 |
func implements(T, V *abi.Type) bool { |
| 1432 |
if T.Kind() != abi.Interface { |
| 1433 |
return false |
| 1434 |
} |
| 1435 |
t := (*interfaceType)(unsafe.Pointer(T)) |
| 1436 |
if len(t.Methods) == 0 { |
| 1437 |
return true |
| 1438 |
} |
| 1439 |
|
| 1440 |
// The same algorithm applies in both cases, but the |
| 1441 |
// method tables for an interface type and a concrete type |
| 1442 |
// are different, so the code is duplicated. |
| 1443 |
// In both cases the algorithm is a linear scan over the two |
| 1444 |
// lists - T's methods and V's methods - simultaneously. |
| 1445 |
// Since method tables are stored in a unique sorted order |
| 1446 |
// (alphabetical, with no duplicate method names), the scan |
| 1447 |
// through V's methods must hit a match for each of T's |
| 1448 |
// methods along the way, or else V does not implement T. |
| 1449 |
// This lets us run the scan in overall linear time instead of |
| 1450 |
// the quadratic time a naive search would require. |
| 1451 |
// See also ../runtime/iface.go. |
| 1452 |
if V.Kind() == abi.Interface { |
| 1453 |
v := (*interfaceType)(unsafe.Pointer(V)) |
| 1454 |
i := 0 |
| 1455 |
for j := 0; j < len(v.Methods); j++ { |
| 1456 |
tm := &t.Methods[i] |
| 1457 |
tmName := t.nameOff(tm.Name) |
| 1458 |
vm := &v.Methods[j] |
| 1459 |
vmName := nameOffFor(V, vm.Name) |
| 1460 |
if vmName.Name() == tmName.Name() && typeOffFor(V, vm.Typ) == t.typeOff(tm.Typ) { |
| 1461 |
if !tmName.IsExported() { |
| 1462 |
tmPkgPath := pkgPath(tmName) |
| 1463 |
if tmPkgPath == "" { |
| 1464 |
tmPkgPath = t.PkgPath.Name() |
| 1465 |
} |
| 1466 |
vmPkgPath := pkgPath(vmName) |
| 1467 |
if vmPkgPath == "" { |
| 1468 |
vmPkgPath = v.PkgPath.Name() |
| 1469 |
} |
| 1470 |
if tmPkgPath != vmPkgPath { |
| 1471 |
continue |
| 1472 |
} |
| 1473 |
} |
| 1474 |
if i++; i >= len(t.Methods) { |
| 1475 |
return true |
| 1476 |
} |
| 1477 |
} |
| 1478 |
} |
| 1479 |
return false |
| 1480 |
} |
| 1481 |
|
| 1482 |
v := V.Uncommon() |
| 1483 |
if v == nil { |
| 1484 |
return false |
| 1485 |
} |
| 1486 |
i := 0 |
| 1487 |
vmethods := v.Methods() |
| 1488 |
for j := 0; j < int(v.Mcount); j++ { |
| 1489 |
tm := &t.Methods[i] |
| 1490 |
tmName := t.nameOff(tm.Name) |
| 1491 |
vm := vmethods[j] |
| 1492 |
vmName := nameOffFor(V, vm.Name) |
| 1493 |
if vmName.Name() == tmName.Name() && typeOffFor(V, vm.Mtyp) == t.typeOff(tm.Typ) { |
| 1494 |
if !tmName.IsExported() { |
| 1495 |
tmPkgPath := pkgPath(tmName) |
| 1496 |
if tmPkgPath == "" { |
| 1497 |
tmPkgPath = t.PkgPath.Name() |
| 1498 |
} |
| 1499 |
vmPkgPath := pkgPath(vmName) |
| 1500 |
if vmPkgPath == "" { |
| 1501 |
vmPkgPath = nameOffFor(V, v.PkgPath).Name() |
| 1502 |
} |
| 1503 |
if tmPkgPath != vmPkgPath { |
| 1504 |
continue |
| 1505 |
} |
| 1506 |
} |
| 1507 |
if i++; i >= len(t.Methods) { |
| 1508 |
return true |
| 1509 |
} |
| 1510 |
} |
| 1511 |
} |
| 1512 |
return false |
| 1513 |
} |
| 1514 |
|
| 1515 |
// specialChannelAssignability reports whether a value x of channel type V |
| 1516 |
// can be directly assigned (using memmove) to another channel type T. |
| 1517 |
// https://golang.org/doc/go_spec.html#Assignability |
| 1518 |
// T and V must be both of Chan kind. |
| 1519 |
func specialChannelAssignability(T, V *abi.Type) bool { |
| 1520 |
// Special case: |
| 1521 |
// x is a bidirectional channel value, T is a channel type, |
| 1522 |
// x's type V and T have identical element types, |
| 1523 |
// and at least one of V or T is not a defined type. |
| 1524 |
return V.ChanDir() == abi.BothDir && (nameFor(T) == "" || nameFor(V) == "") && haveIdenticalType(T.Elem(), V.Elem(), true) |
| 1525 |
} |
| 1526 |
|
| 1527 |
// directlyAssignable reports whether a value x of type V can be directly |
| 1528 |
// assigned (using memmove) to a value of type T. |
| 1529 |
// https://golang.org/doc/go_spec.html#Assignability |
| 1530 |
// Ignoring the interface rules (implemented elsewhere) |
| 1531 |
// and the ideal constant rules (no ideal constants at run time). |
| 1532 |
func directlyAssignable(T, V *abi.Type) bool { |
| 1533 |
// x's type V is identical to T? |
| 1534 |
if T == V { |
| 1535 |
return true |
| 1536 |
} |
| 1537 |
|
| 1538 |
// Otherwise at least one of T and V must not be defined |
| 1539 |
// and they must have the same kind. |
| 1540 |
if T.HasName() && V.HasName() || T.Kind() != V.Kind() { |
| 1541 |
return false |
| 1542 |
} |
| 1543 |
|
| 1544 |
if T.Kind() == abi.Chan && specialChannelAssignability(T, V) { |
| 1545 |
return true |
| 1546 |
} |
| 1547 |
|
| 1548 |
// x's type T and V must have identical underlying types. |
| 1549 |
return haveIdenticalUnderlyingType(T, V, true) |
| 1550 |
} |
| 1551 |
|
| 1552 |
func haveIdenticalType(T, V *abi.Type, cmpTags bool) bool { |
| 1553 |
if cmpTags { |
| 1554 |
return T == V |
| 1555 |
} |
| 1556 |
|
| 1557 |
if nameFor(T) != nameFor(V) || T.Kind() != V.Kind() || pkgPathFor(T) != pkgPathFor(V) { |
| 1558 |
return false |
| 1559 |
} |
| 1560 |
|
| 1561 |
return haveIdenticalUnderlyingType(T, V, false) |
| 1562 |
} |
| 1563 |
|
| 1564 |
func haveIdenticalUnderlyingType(T, V *abi.Type, cmpTags bool) bool { |
| 1565 |
if T == V { |
| 1566 |
return true |
| 1567 |
} |
| 1568 |
|
| 1569 |
kind := Kind(T.Kind()) |
| 1570 |
if kind != Kind(V.Kind()) { |
| 1571 |
return false |
| 1572 |
} |
| 1573 |
|
| 1574 |
// Non-composite types of equal kind have same underlying type |
| 1575 |
// (the predefined instance of the type). |
| 1576 |
if Bool <= kind && kind <= Complex128 || kind == String || kind == UnsafePointer { |
| 1577 |
return true |
| 1578 |
} |
| 1579 |
|
| 1580 |
// Composite types. |
| 1581 |
switch kind { |
| 1582 |
case Array: |
| 1583 |
return T.Len() == V.Len() && haveIdenticalType(T.Elem(), V.Elem(), cmpTags) |
| 1584 |
|
| 1585 |
case Chan: |
| 1586 |
return V.ChanDir() == T.ChanDir() && haveIdenticalType(T.Elem(), V.Elem(), cmpTags) |
| 1587 |
|
| 1588 |
case Func: |
| 1589 |
t := (*funcType)(unsafe.Pointer(T)) |
| 1590 |
v := (*funcType)(unsafe.Pointer(V)) |
| 1591 |
if t.OutCount != v.OutCount || t.InCount != v.InCount { |
| 1592 |
return false |
| 1593 |
} |
| 1594 |
for i := 0; i < t.NumIn(); i++ { |
| 1595 |
if !haveIdenticalType(t.In(i), v.In(i), cmpTags) { |
| 1596 |
return false |
| 1597 |
} |
| 1598 |
} |
| 1599 |
for i := 0; i < t.NumOut(); i++ { |
| 1600 |
if !haveIdenticalType(t.Out(i), v.Out(i), cmpTags) { |
| 1601 |
return false |
| 1602 |
} |
| 1603 |
} |
| 1604 |
return true |
| 1605 |
|
| 1606 |
case Interface: |
| 1607 |
t := (*interfaceType)(unsafe.Pointer(T)) |
| 1608 |
v := (*interfaceType)(unsafe.Pointer(V)) |
| 1609 |
if len(t.Methods) == 0 && len(v.Methods) == 0 { |
| 1610 |
return true |
| 1611 |
} |
| 1612 |
// Might have the same methods but still |
| 1613 |
// need a run time conversion. |
| 1614 |
return false |
| 1615 |
|
| 1616 |
case Map: |
| 1617 |
return haveIdenticalType(T.Key(), V.Key(), cmpTags) && haveIdenticalType(T.Elem(), V.Elem(), cmpTags) |
| 1618 |
|
| 1619 |
case Pointer, Slice: |
| 1620 |
return haveIdenticalType(T.Elem(), V.Elem(), cmpTags) |
| 1621 |
|
| 1622 |
case Struct: |
| 1623 |
t := (*structType)(unsafe.Pointer(T)) |
| 1624 |
v := (*structType)(unsafe.Pointer(V)) |
| 1625 |
if len(t.Fields) != len(v.Fields) { |
| 1626 |
return false |
| 1627 |
} |
| 1628 |
if t.PkgPath.Name() != v.PkgPath.Name() { |
| 1629 |
return false |
| 1630 |
} |
| 1631 |
for i := range t.Fields { |
| 1632 |
tf := &t.Fields[i] |
| 1633 |
vf := &v.Fields[i] |
| 1634 |
if tf.Name.Name() != vf.Name.Name() { |
| 1635 |
return false |
| 1636 |
} |
| 1637 |
if !haveIdenticalType(tf.Typ, vf.Typ, cmpTags) { |
| 1638 |
return false |
| 1639 |
} |
| 1640 |
if cmpTags && tf.Name.Tag() != vf.Name.Tag() { |
| 1641 |
return false |
| 1642 |
} |
| 1643 |
if tf.Offset != vf.Offset { |
| 1644 |
return false |
| 1645 |
} |
| 1646 |
if tf.Embedded() != vf.Embedded() { |
| 1647 |
return false |
| 1648 |
} |
| 1649 |
} |
| 1650 |
return true |
| 1651 |
} |
| 1652 |
|
| 1653 |
return false |
| 1654 |
} |
| 1655 |
|
| 1656 |
// typelinks is implemented in package runtime. |
| 1657 |
// It returns a slice of the sections in each module, |
| 1658 |
// and a slice of *rtype offsets in each module. |
| 1659 |
// |
| 1660 |
// The types in each module are sorted by string. That is, the first |
| 1661 |
// two linked types of the first module are: |
| 1662 |
// |
| 1663 |
// d0 := sections[0] |
| 1664 |
// t1 := (*rtype)(add(d0, offset[0][0])) |
| 1665 |
// t2 := (*rtype)(add(d0, offset[0][1])) |
| 1666 |
// |
| 1667 |
// and |
| 1668 |
// |
| 1669 |
// t1.String() < t2.String() |
| 1670 |
// |
| 1671 |
// Note that strings are not unique identifiers for types: |
| 1672 |
// there can be more than one with a given string. |
| 1673 |
// Only types we might want to look up are included: |
| 1674 |
// pointers, channels, maps, slices, and arrays. |
| 1675 |
func typelinks() (sections []unsafe.Pointer, offset [][]int32) |
| 1676 |
|
| 1677 |
// rtypeOff should be an internal detail, |
| 1678 |
// but widely used packages access it using linkname. |
| 1679 |
// Notable members of the hall of shame include: |
| 1680 |
// - github.com/goccy/go-json |
| 1681 |
// |
| 1682 |
// Do not remove or change the type signature. |
| 1683 |
// See go.dev/issue/67401. |
| 1684 |
// |
| 1685 |
//go:linkname rtypeOff |
| 1686 |
func rtypeOff(section unsafe.Pointer, off int32) *abi.Type { |
| 1687 |
return (*abi.Type)(add(section, uintptr(off), "sizeof(rtype) > 0")) |
| 1688 |
} |
| 1689 |
|
| 1690 |
// typesByString returns the subslice of typelinks() whose elements have |
| 1691 |
// the given string representation. |
| 1692 |
// It may be empty (no known types with that string) or may have |
| 1693 |
// multiple elements (multiple types with that string). |
| 1694 |
// |
| 1695 |
// typesByString should be an internal detail, |
| 1696 |
// but widely used packages access it using linkname. |
| 1697 |
// Notable members of the hall of shame include: |
| 1698 |
// - github.com/aristanetworks/goarista |
| 1699 |
// - fortio.org/log |
| 1700 |
// |
| 1701 |
// Do not remove or change the type signature. |
| 1702 |
// See go.dev/issue/67401. |
| 1703 |
// |
| 1704 |
//go:linkname typesByString |
| 1705 |
func typesByString(s string) []*abi.Type { |
| 1706 |
sections, offset := typelinks() |
| 1707 |
var ret []*abi.Type |
| 1708 |
|
| 1709 |
for offsI, offs := range offset { |
| 1710 |
section := sections[offsI] |
| 1711 |
|
| 1712 |
// We are looking for the first index i where the string becomes >= s. |
| 1713 |
// This is a copy of sort.Search, with f(h) replaced by (*typ[h].String() >= s). |
| 1714 |
i, j := 0, len(offs) |
| 1715 |
for i < j { |
| 1716 |
h := int(uint(i+j) >> 1) // avoid overflow when computing h |
| 1717 |
// i ≤ h < j |
| 1718 |
if !(stringFor(rtypeOff(section, offs[h])) >= s) { |
| 1719 |
i = h + 1 // preserves f(i-1) == false |
| 1720 |
} else { |
| 1721 |
j = h // preserves f(j) == true |
| 1722 |
} |
| 1723 |
} |
| 1724 |
// i == j, f(i-1) == false, and f(j) (= f(i)) == true => answer is i. |
| 1725 |
|
| 1726 |
// Having found the first, linear scan forward to find the last. |
| 1727 |
// We could do a second binary search, but the caller is going |
| 1728 |
// to do a linear scan anyway. |
| 1729 |
for j := i; j < len(offs); j++ { |
| 1730 |
typ := rtypeOff(section, offs[j]) |
| 1731 |
if stringFor(typ) != s { |
| 1732 |
break |
| 1733 |
} |
| 1734 |
ret = append(ret, typ) |
| 1735 |
} |
| 1736 |
} |
| 1737 |
return ret |
| 1738 |
} |
| 1739 |
|
| 1740 |
// The lookupCache caches ArrayOf, ChanOf, MapOf and SliceOf lookups. |
| 1741 |
var lookupCache sync.Map // map[cacheKey]*rtype |
| 1742 |
|
| 1743 |
// A cacheKey is the key for use in the lookupCache. |
| 1744 |
// Four values describe any of the types we are looking for: |
| 1745 |
// type kind, one or two subtypes, and an extra integer. |
| 1746 |
type cacheKey struct { |
| 1747 |
kind Kind |
| 1748 |
t1 *abi.Type |
| 1749 |
t2 *abi.Type |
| 1750 |
extra uintptr |
| 1751 |
} |
| 1752 |
|
| 1753 |
// The funcLookupCache caches FuncOf lookups. |
| 1754 |
// FuncOf does not share the common lookupCache since cacheKey is not |
| 1755 |
// sufficient to represent functions unambiguously. |
| 1756 |
var funcLookupCache struct { |
| 1757 |
sync.Mutex // Guards stores (but not loads) on m. |
| 1758 |
|
| 1759 |
// m is a map[uint32][]*rtype keyed by the hash calculated in FuncOf. |
| 1760 |
// Elements of m are append-only and thus safe for concurrent reading. |
| 1761 |
m sync.Map |
| 1762 |
} |
| 1763 |
|
| 1764 |
// ChanOf returns the channel type with the given direction and element type. |
| 1765 |
// For example, if t represents int, ChanOf(RecvDir, t) represents <-chan int. |
| 1766 |
// |
| 1767 |
// The gc runtime imposes a limit of 64 kB on channel element types. |
| 1768 |
// If t's size is equal to or exceeds this limit, ChanOf panics. |
| 1769 |
func ChanOf(dir ChanDir, t Type) Type { |
| 1770 |
typ := t.common() |
| 1771 |
|
| 1772 |
// Look in cache. |
| 1773 |
ckey := cacheKey{Chan, typ, nil, uintptr(dir)} |
| 1774 |
if ch, ok := lookupCache.Load(ckey); ok { |
| 1775 |
return ch.(*rtype) |
| 1776 |
} |
| 1777 |
|
| 1778 |
// This restriction is imposed by the gc compiler and the runtime. |
| 1779 |
if typ.Size_ >= 1<<16 { |
| 1780 |
panic("reflect.ChanOf: element size too large") |
| 1781 |
} |
| 1782 |
|
| 1783 |
// Look in known types. |
| 1784 |
var s string |
| 1785 |
switch dir { |
| 1786 |
default: |
| 1787 |
panic("reflect.ChanOf: invalid dir") |
| 1788 |
case SendDir: |
| 1789 |
s = "chan<- " + stringFor(typ) |
| 1790 |
case RecvDir: |
| 1791 |
s = "<-chan " + stringFor(typ) |
| 1792 |
case BothDir: |
| 1793 |
typeStr := stringFor(typ) |
| 1794 |
if typeStr[0] == '<' { |
| 1795 |
// typ is recv chan, need parentheses as "<-" associates with leftmost |
| 1796 |
// chan possible, see: |
| 1797 |
// * https://golang.org/ref/spec#Channel_types |
| 1798 |
// * https://github.com/golang/go/issues/39897 |
| 1799 |
s = "chan (" + typeStr + ")" |
| 1800 |
} else { |
| 1801 |
s = "chan " + typeStr |
| 1802 |
} |
| 1803 |
} |
| 1804 |
for _, tt := range typesByString(s) { |
| 1805 |
ch := (*chanType)(unsafe.Pointer(tt)) |
| 1806 |
if ch.Elem == typ && ch.Dir == abi.ChanDir(dir) { |
| 1807 |
ti, _ := lookupCache.LoadOrStore(ckey, toRType(tt)) |
| 1808 |
return ti.(Type) |
| 1809 |
} |
| 1810 |
} |
| 1811 |
|
| 1812 |
// Make a channel type. |
| 1813 |
var ichan any = (chan unsafe.Pointer)(nil) |
| 1814 |
prototype := *(**chanType)(unsafe.Pointer(&ichan)) |
| 1815 |
ch := *prototype |
| 1816 |
ch.TFlag = abi.TFlagRegularMemory |
| 1817 |
ch.Dir = abi.ChanDir(dir) |
| 1818 |
ch.Str = resolveReflectName(newName(s, "", false, false)) |
| 1819 |
ch.Hash = fnv1(typ.Hash, 'c', byte(dir)) |
| 1820 |
ch.Elem = typ |
| 1821 |
|
| 1822 |
ti, _ := lookupCache.LoadOrStore(ckey, toRType(&ch.Type)) |
| 1823 |
return ti.(Type) |
| 1824 |
} |
| 1825 |
|
| 1826 |
var funcTypes []Type |
| 1827 |
var funcTypesMutex sync.Mutex |
| 1828 |
|
| 1829 |
func initFuncTypes(n int) Type { |
| 1830 |
funcTypesMutex.Lock() |
| 1831 |
defer funcTypesMutex.Unlock() |
| 1832 |
if n >= len(funcTypes) { |
| 1833 |
newFuncTypes := make([]Type, n+1) |
| 1834 |
copy(newFuncTypes, funcTypes) |
| 1835 |
funcTypes = newFuncTypes |
| 1836 |
} |
| 1837 |
if funcTypes[n] != nil { |
| 1838 |
return funcTypes[n] |
| 1839 |
} |
| 1840 |
|
| 1841 |
funcTypes[n] = StructOf([]StructField{ |
| 1842 |
{ |
| 1843 |
Name: "FuncType", |
| 1844 |
Type: TypeOf(funcType{}), |
| 1845 |
}, |
| 1846 |
{ |
| 1847 |
Name: "Args", |
| 1848 |
Type: ArrayOf(n, TypeOf(&rtype{})), |
| 1849 |
}, |
| 1850 |
}) |
| 1851 |
return funcTypes[n] |
| 1852 |
} |
| 1853 |
|
| 1854 |
// FuncOf returns the function type with the given argument and result types. |
| 1855 |
// For example if k represents int and e represents string, |
| 1856 |
// FuncOf([]Type{k}, []Type{e}, false) represents func(int) string. |
| 1857 |
// |
| 1858 |
// The variadic argument controls whether the function is variadic. FuncOf |
| 1859 |
// panics if the in[len(in)-1] does not represent a slice and variadic is |
| 1860 |
// true. |
| 1861 |
func FuncOf(in, out []Type, variadic bool) Type { |
| 1862 |
if variadic && (len(in) == 0 || in[len(in)-1].Kind() != Slice) { |
| 1863 |
panic("reflect.FuncOf: last arg of variadic func must be slice") |
| 1864 |
} |
| 1865 |
|
| 1866 |
// Make a func type. |
| 1867 |
var ifunc any = (func())(nil) |
| 1868 |
prototype := *(**funcType)(unsafe.Pointer(&ifunc)) |
| 1869 |
n := len(in) + len(out) |
| 1870 |
|
| 1871 |
if n > 128 { |
| 1872 |
panic("reflect.FuncOf: too many arguments") |
| 1873 |
} |
| 1874 |
|
| 1875 |
o := New(initFuncTypes(n)).Elem() |
| 1876 |
ft := (*funcType)(unsafe.Pointer(o.Field(0).Addr().Pointer())) |
| 1877 |
args := unsafe.Slice((**rtype)(unsafe.Pointer(o.Field(1).Addr().Pointer())), n)[0:0:n] |
| 1878 |
*ft = *prototype |
| 1879 |
|
| 1880 |
// Build a hash and minimally populate ft. |
| 1881 |
var hash uint32 |
| 1882 |
for _, in := range in { |
| 1883 |
t := in.(*rtype) |
| 1884 |
args = append(args, t) |
| 1885 |
hash = fnv1(hash, byte(t.t.Hash>>24), byte(t.t.Hash>>16), byte(t.t.Hash>>8), byte(t.t.Hash)) |
| 1886 |
} |
| 1887 |
if variadic { |
| 1888 |
hash = fnv1(hash, 'v') |
| 1889 |
} |
| 1890 |
hash = fnv1(hash, '.') |
| 1891 |
for _, out := range out { |
| 1892 |
t := out.(*rtype) |
| 1893 |
args = append(args, t) |
| 1894 |
hash = fnv1(hash, byte(t.t.Hash>>24), byte(t.t.Hash>>16), byte(t.t.Hash>>8), byte(t.t.Hash)) |
| 1895 |
} |
| 1896 |
|
| 1897 |
ft.TFlag = 0 |
| 1898 |
ft.Hash = hash |
| 1899 |
ft.InCount = uint16(len(in)) |
| 1900 |
ft.OutCount = uint16(len(out)) |
| 1901 |
if variadic { |
| 1902 |
ft.OutCount |= 1 << 15 |
| 1903 |
} |
| 1904 |
|
| 1905 |
// Look in cache. |
| 1906 |
if ts, ok := funcLookupCache.m.Load(hash); ok { |
| 1907 |
for _, t := range ts.([]*abi.Type) { |
| 1908 |
if haveIdenticalUnderlyingType(&ft.Type, t, true) { |
| 1909 |
return toRType(t) |
| 1910 |
} |
| 1911 |
} |
| 1912 |
} |
| 1913 |
|
| 1914 |
// Not in cache, lock and retry. |
| 1915 |
funcLookupCache.Lock() |
| 1916 |
defer funcLookupCache.Unlock() |
| 1917 |
if ts, ok := funcLookupCache.m.Load(hash); ok { |
| 1918 |
for _, t := range ts.([]*abi.Type) { |
| 1919 |
if haveIdenticalUnderlyingType(&ft.Type, t, true) { |
| 1920 |
return toRType(t) |
| 1921 |
} |
| 1922 |
} |
| 1923 |
} |
| 1924 |
|
| 1925 |
addToCache := func(tt *abi.Type) Type { |
| 1926 |
var rts []*abi.Type |
| 1927 |
if rti, ok := funcLookupCache.m.Load(hash); ok { |
| 1928 |
rts = rti.([]*abi.Type) |
| 1929 |
} |
| 1930 |
funcLookupCache.m.Store(hash, append(rts, tt)) |
| 1931 |
return toType(tt) |
| 1932 |
} |
| 1933 |
|
| 1934 |
// Look in known types for the same string representation. |
| 1935 |
str := funcStr(ft) |
| 1936 |
for _, tt := range typesByString(str) { |
| 1937 |
if haveIdenticalUnderlyingType(&ft.Type, tt, true) { |
| 1938 |
return addToCache(tt) |
| 1939 |
} |
| 1940 |
} |
| 1941 |
|
| 1942 |
// Populate the remaining fields of ft and store in cache. |
| 1943 |
ft.Str = resolveReflectName(newName(str, "", false, false)) |
| 1944 |
ft.PtrToThis = 0 |
| 1945 |
return addToCache(&ft.Type) |
| 1946 |
} |
| 1947 |
func stringFor(t *abi.Type) string { |
| 1948 |
return toRType(t).String() |
| 1949 |
} |
| 1950 |
|
| 1951 |
// funcStr builds a string representation of a funcType. |
| 1952 |
func funcStr(ft *funcType) string { |
| 1953 |
repr := make([]byte, 0, 64) |
| 1954 |
repr = append(repr, "func("...) |
| 1955 |
for i, t := range ft.InSlice() { |
| 1956 |
if i > 0 { |
| 1957 |
repr = append(repr, ", "...) |
| 1958 |
} |
| 1959 |
if ft.IsVariadic() && i == int(ft.InCount)-1 { |
| 1960 |
repr = append(repr, "..."...) |
| 1961 |
repr = append(repr, stringFor((*sliceType)(unsafe.Pointer(t)).Elem)...) |
| 1962 |
} else { |
| 1963 |
repr = append(repr, stringFor(t)...) |
| 1964 |
} |
| 1965 |
} |
| 1966 |
repr = append(repr, ')') |
| 1967 |
out := ft.OutSlice() |
| 1968 |
if len(out) == 1 { |
| 1969 |
repr = append(repr, ' ') |
| 1970 |
} else if len(out) > 1 { |
| 1971 |
repr = append(repr, " ("...) |
| 1972 |
} |
| 1973 |
for i, t := range out { |
| 1974 |
if i > 0 { |
| 1975 |
repr = append(repr, ", "...) |
| 1976 |
} |
| 1977 |
repr = append(repr, stringFor(t)...) |
| 1978 |
} |
| 1979 |
if len(out) > 1 { |
| 1980 |
repr = append(repr, ')') |
| 1981 |
} |
| 1982 |
return string(repr) |
| 1983 |
} |
| 1984 |
|
| 1985 |
// isReflexive reports whether the == operation on the type is reflexive. |
| 1986 |
// That is, x == x for all values x of type t. |
| 1987 |
func isReflexive(t *abi.Type) bool { |
| 1988 |
switch Kind(t.Kind()) { |
| 1989 |
case Bool, Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64, Uintptr, Chan, Pointer, String, UnsafePointer: |
| 1990 |
return true |
| 1991 |
case Float32, Float64, Complex64, Complex128, Interface: |
| 1992 |
return false |
| 1993 |
case Array: |
| 1994 |
tt := (*arrayType)(unsafe.Pointer(t)) |
| 1995 |
return isReflexive(tt.Elem) |
| 1996 |
case Struct: |
| 1997 |
tt := (*structType)(unsafe.Pointer(t)) |
| 1998 |
for _, f := range tt.Fields { |
| 1999 |
if !isReflexive(f.Typ) { |
| 2000 |
return false |
| 2001 |
} |
| 2002 |
} |
| 2003 |
return true |
| 2004 |
default: |
| 2005 |
// Func, Map, Slice, Invalid |
| 2006 |
panic("isReflexive called on non-key type " + stringFor(t)) |
| 2007 |
} |
| 2008 |
} |
| 2009 |
|
| 2010 |
// needKeyUpdate reports whether map overwrites require the key to be copied. |
| 2011 |
func needKeyUpdate(t *abi.Type) bool { |
| 2012 |
switch Kind(t.Kind()) { |
| 2013 |
case Bool, Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64, Uintptr, Chan, Pointer, UnsafePointer: |
| 2014 |
return false |
| 2015 |
case Float32, Float64, Complex64, Complex128, Interface, String: |
| 2016 |
// Float keys can be updated from +0 to -0. |
| 2017 |
// String keys can be updated to use a smaller backing store. |
| 2018 |
// Interfaces might have floats or strings in them. |
| 2019 |
return true |
| 2020 |
case Array: |
| 2021 |
tt := (*arrayType)(unsafe.Pointer(t)) |
| 2022 |
return needKeyUpdate(tt.Elem) |
| 2023 |
case Struct: |
| 2024 |
tt := (*structType)(unsafe.Pointer(t)) |
| 2025 |
for _, f := range tt.Fields { |
| 2026 |
if needKeyUpdate(f.Typ) { |
| 2027 |
return true |
| 2028 |
} |
| 2029 |
} |
| 2030 |
return false |
| 2031 |
default: |
| 2032 |
// Func, Map, Slice, Invalid |
| 2033 |
panic("needKeyUpdate called on non-key type " + stringFor(t)) |
| 2034 |
} |
| 2035 |
} |
| 2036 |
|
| 2037 |
// hashMightPanic reports whether the hash of a map key of type t might panic. |
| 2038 |
func hashMightPanic(t *abi.Type) bool { |
| 2039 |
switch Kind(t.Kind()) { |
| 2040 |
case Interface: |
| 2041 |
return true |
| 2042 |
case Array: |
| 2043 |
tt := (*arrayType)(unsafe.Pointer(t)) |
| 2044 |
return hashMightPanic(tt.Elem) |
| 2045 |
case Struct: |
| 2046 |
tt := (*structType)(unsafe.Pointer(t)) |
| 2047 |
for _, f := range tt.Fields { |
| 2048 |
if hashMightPanic(f.Typ) { |
| 2049 |
return true |
| 2050 |
} |
| 2051 |
} |
| 2052 |
return false |
| 2053 |
default: |
| 2054 |
return false |
| 2055 |
} |
| 2056 |
} |
| 2057 |
|
| 2058 |
// emitGCMask writes the GC mask for [n]typ into out, starting at bit |
| 2059 |
// offset base. |
| 2060 |
func emitGCMask(out []byte, base uintptr, typ *abi.Type, n uintptr) { |
| 2061 |
ptrs := typ.PtrBytes / goarch.PtrSize |
| 2062 |
words := typ.Size_ / goarch.PtrSize |
| 2063 |
mask := typ.GcSlice(0, (ptrs+7)/8) |
| 2064 |
for j := uintptr(0); j < ptrs; j++ { |
| 2065 |
if (mask[j/8]>>(j%8))&1 != 0 { |
| 2066 |
for i := uintptr(0); i < n; i++ { |
| 2067 |
k := base + i*words + j |
| 2068 |
out[k/8] |= 1 << (k % 8) |
| 2069 |
} |
| 2070 |
} |
| 2071 |
} |
| 2072 |
} |
| 2073 |
|
| 2074 |
// SliceOf returns the slice type with element type t. |
| 2075 |
// For example, if t represents int, SliceOf(t) represents []int. |
| 2076 |
func SliceOf(t Type) Type { |
| 2077 |
typ := t.common() |
| 2078 |
|
| 2079 |
// Look in cache. |
| 2080 |
ckey := cacheKey{Slice, typ, nil, 0} |
| 2081 |
if slice, ok := lookupCache.Load(ckey); ok { |
| 2082 |
return slice.(Type) |
| 2083 |
} |
| 2084 |
|
| 2085 |
// Look in known types. |
| 2086 |
s := "[]" + stringFor(typ) |
| 2087 |
for _, tt := range typesByString(s) { |
| 2088 |
slice := (*sliceType)(unsafe.Pointer(tt)) |
| 2089 |
if slice.Elem == typ { |
| 2090 |
ti, _ := lookupCache.LoadOrStore(ckey, toRType(tt)) |
| 2091 |
return ti.(Type) |
| 2092 |
} |
| 2093 |
} |
| 2094 |
|
| 2095 |
// Make a slice type. |
| 2096 |
var islice any = ([]unsafe.Pointer)(nil) |
| 2097 |
prototype := *(**sliceType)(unsafe.Pointer(&islice)) |
| 2098 |
slice := *prototype |
| 2099 |
slice.TFlag = 0 |
| 2100 |
slice.Str = resolveReflectName(newName(s, "", false, false)) |
| 2101 |
slice.Hash = fnv1(typ.Hash, '[') |
| 2102 |
slice.Elem = typ |
| 2103 |
slice.PtrToThis = 0 |
| 2104 |
|
| 2105 |
ti, _ := lookupCache.LoadOrStore(ckey, toRType(&slice.Type)) |
| 2106 |
return ti.(Type) |
| 2107 |
} |
| 2108 |
|
| 2109 |
// The structLookupCache caches StructOf lookups. |
| 2110 |
// StructOf does not share the common lookupCache since we need to pin |
| 2111 |
// the memory associated with *structTypeFixedN. |
| 2112 |
var structLookupCache struct { |
| 2113 |
sync.Mutex // Guards stores (but not loads) on m. |
| 2114 |
|
| 2115 |
// m is a map[uint32][]Type keyed by the hash calculated in StructOf. |
| 2116 |
// Elements in m are append-only and thus safe for concurrent reading. |
| 2117 |
m sync.Map |
| 2118 |
} |
| 2119 |
|
| 2120 |
type structTypeUncommon struct { |
| 2121 |
structType |
| 2122 |
u uncommonType |
| 2123 |
} |
| 2124 |
|
| 2125 |
// isLetter reports whether a given 'rune' is classified as a Letter. |
| 2126 |
func isLetter(ch rune) bool { |
| 2127 |
return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' || ch >= utf8.RuneSelf && unicode.IsLetter(ch) |
| 2128 |
} |
| 2129 |
|
| 2130 |
// isValidFieldName checks if a string is a valid (struct) field name or not. |
| 2131 |
// |
| 2132 |
// According to the language spec, a field name should be an identifier. |
| 2133 |
// |
| 2134 |
// identifier = letter { letter | unicode_digit } . |
| 2135 |
// letter = unicode_letter | "_" . |
| 2136 |
func isValidFieldName(fieldName string) bool { |
| 2137 |
for i, c := range fieldName { |
| 2138 |
if i == 0 && !isLetter(c) { |
| 2139 |
return false |
| 2140 |
} |
| 2141 |
|
| 2142 |
if !(isLetter(c) || unicode.IsDigit(c)) { |
| 2143 |
return false |
| 2144 |
} |
| 2145 |
} |
| 2146 |
|
| 2147 |
return len(fieldName) > 0 |
| 2148 |
} |
| 2149 |
|
| 2150 |
// This must match cmd/compile/internal/compare.IsRegularMemory |
| 2151 |
func isRegularMemory(t Type) bool { |
| 2152 |
switch t.Kind() { |
| 2153 |
case Array: |
| 2154 |
elem := t.Elem() |
| 2155 |
if isRegularMemory(elem) { |
| 2156 |
return true |
| 2157 |
} |
| 2158 |
return elem.Comparable() && t.Len() == 0 |
| 2159 |
case Int8, Int16, Int32, Int64, Int, Uint8, Uint16, Uint32, Uint64, Uint, Uintptr, Chan, Pointer, Bool, UnsafePointer: |
| 2160 |
return true |
| 2161 |
case Struct: |
| 2162 |
num := t.NumField() |
| 2163 |
switch num { |
| 2164 |
case 0: |
| 2165 |
return true |
| 2166 |
case 1: |
| 2167 |
field := t.Field(0) |
| 2168 |
if field.Name == "_" { |
| 2169 |
return false |
| 2170 |
} |
| 2171 |
return isRegularMemory(field.Type) |
| 2172 |
default: |
| 2173 |
for i := range num { |
| 2174 |
field := t.Field(i) |
| 2175 |
if field.Name == "_" || !isRegularMemory(field.Type) || isPaddedField(t, i) { |
| 2176 |
return false |
| 2177 |
} |
| 2178 |
} |
| 2179 |
return true |
| 2180 |
} |
| 2181 |
} |
| 2182 |
return false |
| 2183 |
} |
| 2184 |
|
| 2185 |
// isPaddedField reports whether the i'th field of struct type t is followed |
| 2186 |
// by padding. |
| 2187 |
func isPaddedField(t Type, i int) bool { |
| 2188 |
field := t.Field(i) |
| 2189 |
if i+1 < t.NumField() { |
| 2190 |
return field.Offset+field.Type.Size() != t.Field(i+1).Offset |
| 2191 |
} |
| 2192 |
return field.Offset+field.Type.Size() != t.Size() |
| 2193 |
} |
| 2194 |
|
| 2195 |
// StructOf returns the struct type containing fields. |
| 2196 |
// The Offset and Index fields are ignored and computed as they would be |
| 2197 |
// by the compiler. |
| 2198 |
// |
| 2199 |
// StructOf currently does not support promoted methods of embedded fields |
| 2200 |
// and panics if passed unexported StructFields. |
| 2201 |
func StructOf(fields []StructField) Type { |
| 2202 |
var ( |
| 2203 |
hash = fnv1(0, []byte("struct {")...) |
| 2204 |
size uintptr |
| 2205 |
typalign uint8 |
| 2206 |
comparable = true |
| 2207 |
methods []abi.Method |
| 2208 |
|
| 2209 |
fs = make([]structField, len(fields)) |
| 2210 |
repr = make([]byte, 0, 64) |
| 2211 |
fset = map[string]struct{}{} // fields' names |
| 2212 |
) |
| 2213 |
|
| 2214 |
lastzero := uintptr(0) |
| 2215 |
repr = append(repr, "struct {"...) |
| 2216 |
pkgpath := "" |
| 2217 |
for i, field := range fields { |
| 2218 |
if field.Name == "" { |
| 2219 |
panic("reflect.StructOf: field " + strconv.Itoa(i) + " has no name") |
| 2220 |
} |
| 2221 |
if !isValidFieldName(field.Name) { |
| 2222 |
panic("reflect.StructOf: field " + strconv.Itoa(i) + " has invalid name") |
| 2223 |
} |
| 2224 |
if field.Type == nil { |
| 2225 |
panic("reflect.StructOf: field " + strconv.Itoa(i) + " has no type") |
| 2226 |
} |
| 2227 |
f, fpkgpath := runtimeStructField(field) |
| 2228 |
ft := f.Typ |
| 2229 |
if fpkgpath != "" { |
| 2230 |
if pkgpath == "" { |
| 2231 |
pkgpath = fpkgpath |
| 2232 |
} else if pkgpath != fpkgpath { |
| 2233 |
panic("reflect.Struct: fields with different PkgPath " + pkgpath + " and " + fpkgpath) |
| 2234 |
} |
| 2235 |
} |
| 2236 |
|
| 2237 |
// Update string and hash |
| 2238 |
name := f.Name.Name() |
| 2239 |
hash = fnv1(hash, []byte(name)...) |
| 2240 |
if !f.Embedded() { |
| 2241 |
repr = append(repr, (" " + name)...) |
| 2242 |
} else { |
| 2243 |
// Embedded field |
| 2244 |
if f.Typ.Kind() == abi.Pointer { |
| 2245 |
// Embedded ** and *interface{} are illegal |
| 2246 |
elem := ft.Elem() |
| 2247 |
if k := elem.Kind(); k == abi.Pointer || k == abi.Interface { |
| 2248 |
panic("reflect.StructOf: illegal embedded field type " + stringFor(ft)) |
| 2249 |
} |
| 2250 |
} |
| 2251 |
|
| 2252 |
switch Kind(f.Typ.Kind()) { |
| 2253 |
case Interface: |
| 2254 |
ift := (*interfaceType)(unsafe.Pointer(ft)) |
| 2255 |
for _, m := range ift.Methods { |
| 2256 |
if pkgPath(ift.nameOff(m.Name)) != "" { |
| 2257 |
// TODO(sbinet). Issue 15924. |
| 2258 |
panic("reflect: embedded interface with unexported method(s) not implemented") |
| 2259 |
} |
| 2260 |
|
| 2261 |
fnStub := resolveReflectText(unsafe.Pointer(abi.FuncPCABIInternal(embeddedIfaceMethStub))) |
| 2262 |
methods = append(methods, abi.Method{ |
| 2263 |
Name: resolveReflectName(ift.nameOff(m.Name)), |
| 2264 |
Mtyp: resolveReflectType(ift.typeOff(m.Typ)), |
| 2265 |
Ifn: fnStub, |
| 2266 |
Tfn: fnStub, |
| 2267 |
}) |
| 2268 |
} |
| 2269 |
case Pointer: |
| 2270 |
ptr := (*ptrType)(unsafe.Pointer(ft)) |
| 2271 |
if unt := ptr.Uncommon(); unt != nil { |
| 2272 |
if i > 0 && unt.Mcount > 0 { |
| 2273 |
// Issue 15924. |
| 2274 |
panic("reflect: embedded type with methods not implemented if type is not first field") |
| 2275 |
} |
| 2276 |
if len(fields) > 1 { |
| 2277 |
panic("reflect: embedded type with methods not implemented if there is more than one field") |
| 2278 |
} |
| 2279 |
for _, m := range unt.Methods() { |
| 2280 |
mname := nameOffFor(ft, m.Name) |
| 2281 |
if pkgPath(mname) != "" { |
| 2282 |
// TODO(sbinet). |
| 2283 |
// Issue 15924. |
| 2284 |
panic("reflect: embedded interface with unexported method(s) not implemented") |
| 2285 |
} |
| 2286 |
methods = append(methods, abi.Method{ |
| 2287 |
Name: resolveReflectName(mname), |
| 2288 |
Mtyp: resolveReflectType(typeOffFor(ft, m.Mtyp)), |
| 2289 |
Ifn: resolveReflectText(textOffFor(ft, m.Ifn)), |
| 2290 |
Tfn: resolveReflectText(textOffFor(ft, m.Tfn)), |
| 2291 |
}) |
| 2292 |
} |
| 2293 |
} |
| 2294 |
if unt := ptr.Elem.Uncommon(); unt != nil { |
| 2295 |
for _, m := range unt.Methods() { |
| 2296 |
mname := nameOffFor(ft, m.Name) |
| 2297 |
if pkgPath(mname) != "" { |
| 2298 |
// TODO(sbinet) |
| 2299 |
// Issue 15924. |
| 2300 |
panic("reflect: embedded interface with unexported method(s) not implemented") |
| 2301 |
} |
| 2302 |
methods = append(methods, abi.Method{ |
| 2303 |
Name: resolveReflectName(mname), |
| 2304 |
Mtyp: resolveReflectType(typeOffFor(ptr.Elem, m.Mtyp)), |
| 2305 |
Ifn: resolveReflectText(textOffFor(ptr.Elem, m.Ifn)), |
| 2306 |
Tfn: resolveReflectText(textOffFor(ptr.Elem, m.Tfn)), |
| 2307 |
}) |
| 2308 |
} |
| 2309 |
} |
| 2310 |
default: |
| 2311 |
if unt := ft.Uncommon(); unt != nil { |
| 2312 |
if i > 0 && unt.Mcount > 0 { |
| 2313 |
// Issue 15924. |
| 2314 |
panic("reflect: embedded type with methods not implemented if type is not first field") |
| 2315 |
} |
| 2316 |
if len(fields) > 1 && ft.Kind_&abi.KindDirectIface != 0 { |
| 2317 |
panic("reflect: embedded type with methods not implemented for non-pointer type") |
| 2318 |
} |
| 2319 |
for _, m := range unt.Methods() { |
| 2320 |
mname := nameOffFor(ft, m.Name) |
| 2321 |
if pkgPath(mname) != "" { |
| 2322 |
// TODO(sbinet) |
| 2323 |
// Issue 15924. |
| 2324 |
panic("reflect: embedded interface with unexported method(s) not implemented") |
| 2325 |
} |
| 2326 |
methods = append(methods, abi.Method{ |
| 2327 |
Name: resolveReflectName(mname), |
| 2328 |
Mtyp: resolveReflectType(typeOffFor(ft, m.Mtyp)), |
| 2329 |
Ifn: resolveReflectText(textOffFor(ft, m.Ifn)), |
| 2330 |
Tfn: resolveReflectText(textOffFor(ft, m.Tfn)), |
| 2331 |
}) |
| 2332 |
|
| 2333 |
} |
| 2334 |
} |
| 2335 |
} |
| 2336 |
} |
| 2337 |
if _, dup := fset[name]; dup && name != "_" { |
| 2338 |
panic("reflect.StructOf: duplicate field " + name) |
| 2339 |
} |
| 2340 |
fset[name] = struct{}{} |
| 2341 |
|
| 2342 |
hash = fnv1(hash, byte(ft.Hash>>24), byte(ft.Hash>>16), byte(ft.Hash>>8), byte(ft.Hash)) |
| 2343 |
|
| 2344 |
repr = append(repr, (" " + stringFor(ft))...) |
| 2345 |
if f.Name.HasTag() { |
| 2346 |
hash = fnv1(hash, []byte(f.Name.Tag())...) |
| 2347 |
repr = append(repr, (" " + strconv.Quote(f.Name.Tag()))...) |
| 2348 |
} |
| 2349 |
if i < len(fields)-1 { |
| 2350 |
repr = append(repr, ';') |
| 2351 |
} |
| 2352 |
|
| 2353 |
comparable = comparable && (ft.Equal != nil) |
| 2354 |
|
| 2355 |
offset := align(size, uintptr(ft.Align_)) |
| 2356 |
if offset < size { |
| 2357 |
panic("reflect.StructOf: struct size would exceed virtual address space") |
| 2358 |
} |
| 2359 |
if ft.Align_ > typalign { |
| 2360 |
typalign = ft.Align_ |
| 2361 |
} |
| 2362 |
size = offset + ft.Size_ |
| 2363 |
if size < offset { |
| 2364 |
panic("reflect.StructOf: struct size would exceed virtual address space") |
| 2365 |
} |
| 2366 |
f.Offset = offset |
| 2367 |
|
| 2368 |
if ft.Size_ == 0 { |
| 2369 |
lastzero = size |
| 2370 |
} |
| 2371 |
|
| 2372 |
fs[i] = f |
| 2373 |
} |
| 2374 |
|
| 2375 |
if size > 0 && lastzero == size { |
| 2376 |
// This is a non-zero sized struct that ends in a |
| 2377 |
// zero-sized field. We add an extra byte of padding, |
| 2378 |
// to ensure that taking the address of the final |
| 2379 |
// zero-sized field can't manufacture a pointer to the |
| 2380 |
// next object in the heap. See issue 9401. |
| 2381 |
size++ |
| 2382 |
if size == 0 { |
| 2383 |
panic("reflect.StructOf: struct size would exceed virtual address space") |
| 2384 |
} |
| 2385 |
} |
| 2386 |
|
| 2387 |
var typ *structType |
| 2388 |
var ut *uncommonType |
| 2389 |
|
| 2390 |
if len(methods) == 0 { |
| 2391 |
t := new(structTypeUncommon) |
| 2392 |
typ = &t.structType |
| 2393 |
ut = &t.u |
| 2394 |
} else { |
| 2395 |
// A *rtype representing a struct is followed directly in memory by an |
| 2396 |
// array of method objects representing the methods attached to the |
| 2397 |
// struct. To get the same layout for a run time generated type, we |
| 2398 |
// need an array directly following the uncommonType memory. |
| 2399 |
// A similar strategy is used for funcTypeFixed4, ...funcTypeFixedN. |
| 2400 |
tt := New(StructOf([]StructField{ |
| 2401 |
{Name: "S", Type: TypeOf(structType{})}, |
| 2402 |
{Name: "U", Type: TypeOf(uncommonType{})}, |
| 2403 |
{Name: "M", Type: ArrayOf(len(methods), TypeOf(methods[0]))}, |
| 2404 |
})) |
| 2405 |
|
| 2406 |
typ = (*structType)(tt.Elem().Field(0).Addr().UnsafePointer()) |
| 2407 |
ut = (*uncommonType)(tt.Elem().Field(1).Addr().UnsafePointer()) |
| 2408 |
|
| 2409 |
copy(tt.Elem().Field(2).Slice(0, len(methods)).Interface().([]abi.Method), methods) |
| 2410 |
} |
| 2411 |
// TODO(sbinet): Once we allow embedding multiple types, |
| 2412 |
// methods will need to be sorted like the compiler does. |
| 2413 |
// TODO(sbinet): Once we allow non-exported methods, we will |
| 2414 |
// need to compute xcount as the number of exported methods. |
| 2415 |
ut.Mcount = uint16(len(methods)) |
| 2416 |
ut.Xcount = ut.Mcount |
| 2417 |
ut.Moff = uint32(unsafe.Sizeof(uncommonType{})) |
| 2418 |
|
| 2419 |
if len(fs) > 0 { |
| 2420 |
repr = append(repr, ' ') |
| 2421 |
} |
| 2422 |
repr = append(repr, '}') |
| 2423 |
hash = fnv1(hash, '}') |
| 2424 |
str := string(repr) |
| 2425 |
|
| 2426 |
// Round the size up to be a multiple of the alignment. |
| 2427 |
s := align(size, uintptr(typalign)) |
| 2428 |
if s < size { |
| 2429 |
panic("reflect.StructOf: struct size would exceed virtual address space") |
| 2430 |
} |
| 2431 |
size = s |
| 2432 |
|
| 2433 |
// Make the struct type. |
| 2434 |
var istruct any = struct{}{} |
| 2435 |
prototype := *(**structType)(unsafe.Pointer(&istruct)) |
| 2436 |
*typ = *prototype |
| 2437 |
typ.Fields = fs |
| 2438 |
if pkgpath != "" { |
| 2439 |
typ.PkgPath = newName(pkgpath, "", false, false) |
| 2440 |
} |
| 2441 |
|
| 2442 |
// Look in cache. |
| 2443 |
if ts, ok := structLookupCache.m.Load(hash); ok { |
| 2444 |
for _, st := range ts.([]Type) { |
| 2445 |
t := st.common() |
| 2446 |
if haveIdenticalUnderlyingType(&typ.Type, t, true) { |
| 2447 |
return toType(t) |
| 2448 |
} |
| 2449 |
} |
| 2450 |
} |
| 2451 |
|
| 2452 |
// Not in cache, lock and retry. |
| 2453 |
structLookupCache.Lock() |
| 2454 |
defer structLookupCache.Unlock() |
| 2455 |
if ts, ok := structLookupCache.m.Load(hash); ok { |
| 2456 |
for _, st := range ts.([]Type) { |
| 2457 |
t := st.common() |
| 2458 |
if haveIdenticalUnderlyingType(&typ.Type, t, true) { |
| 2459 |
return toType(t) |
| 2460 |
} |
| 2461 |
} |
| 2462 |
} |
| 2463 |
|
| 2464 |
addToCache := func(t Type) Type { |
| 2465 |
var ts []Type |
| 2466 |
if ti, ok := structLookupCache.m.Load(hash); ok { |
| 2467 |
ts = ti.([]Type) |
| 2468 |
} |
| 2469 |
structLookupCache.m.Store(hash, append(ts, t)) |
| 2470 |
return t |
| 2471 |
} |
| 2472 |
|
| 2473 |
// Look in known types. |
| 2474 |
for _, t := range typesByString(str) { |
| 2475 |
if haveIdenticalUnderlyingType(&typ.Type, t, true) { |
| 2476 |
// even if 't' wasn't a structType with methods, we should be ok |
| 2477 |
// as the 'u uncommonType' field won't be accessed except when |
| 2478 |
// tflag&abi.TFlagUncommon is set. |
| 2479 |
return addToCache(toType(t)) |
| 2480 |
} |
| 2481 |
} |
| 2482 |
|
| 2483 |
typ.Str = resolveReflectName(newName(str, "", false, false)) |
| 2484 |
if isRegularMemory(toType(&typ.Type)) { |
| 2485 |
typ.TFlag = abi.TFlagRegularMemory |
| 2486 |
} else { |
| 2487 |
typ.TFlag = 0 |
| 2488 |
} |
| 2489 |
typ.Hash = hash |
| 2490 |
typ.Size_ = size |
| 2491 |
typ.PtrBytes = typeptrdata(&typ.Type) |
| 2492 |
typ.Align_ = typalign |
| 2493 |
typ.FieldAlign_ = typalign |
| 2494 |
typ.PtrToThis = 0 |
| 2495 |
if len(methods) > 0 { |
| 2496 |
typ.TFlag |= abi.TFlagUncommon |
| 2497 |
} |
| 2498 |
|
| 2499 |
if typ.PtrBytes == 0 { |
| 2500 |
typ.GCData = nil |
| 2501 |
} else if typ.PtrBytes <= abi.MaxPtrmaskBytes*8*goarch.PtrSize { |
| 2502 |
bv := new(bitVector) |
| 2503 |
addTypeBits(bv, 0, &typ.Type) |
| 2504 |
typ.GCData = &bv.data[0] |
| 2505 |
} else { |
| 2506 |
// Runtime will build the mask if needed. We just need to allocate |
| 2507 |
// space to store it. |
| 2508 |
typ.TFlag |= abi.TFlagGCMaskOnDemand |
| 2509 |
typ.GCData = (*byte)(unsafe.Pointer(new(uintptr))) |
| 2510 |
} |
| 2511 |
|
| 2512 |
typ.Equal = nil |
| 2513 |
if comparable { |
| 2514 |
typ.Equal = func(p, q unsafe.Pointer) bool { |
| 2515 |
for _, ft := range typ.Fields { |
| 2516 |
pi := add(p, ft.Offset, "&x.field safe") |
| 2517 |
qi := add(q, ft.Offset, "&x.field safe") |
| 2518 |
if !ft.Typ.Equal(pi, qi) { |
| 2519 |
return false |
| 2520 |
} |
| 2521 |
} |
| 2522 |
return true |
| 2523 |
} |
| 2524 |
} |
| 2525 |
|
| 2526 |
switch { |
| 2527 |
case len(fs) == 1 && !fs[0].Typ.IfaceIndir(): |
| 2528 |
// structs of 1 direct iface type can be direct |
| 2529 |
typ.Kind_ |= abi.KindDirectIface |
| 2530 |
default: |
| 2531 |
typ.Kind_ &^= abi.KindDirectIface |
| 2532 |
} |
| 2533 |
|
| 2534 |
return addToCache(toType(&typ.Type)) |
| 2535 |
} |
| 2536 |
|
| 2537 |
func embeddedIfaceMethStub() { |
| 2538 |
panic("reflect: StructOf does not support methods of embedded interfaces") |
| 2539 |
} |
| 2540 |
|
| 2541 |
// runtimeStructField takes a StructField value passed to StructOf and |
| 2542 |
// returns both the corresponding internal representation, of type |
| 2543 |
// structField, and the pkgpath value to use for this field. |
| 2544 |
func runtimeStructField(field StructField) (structField, string) { |
| 2545 |
if field.Anonymous && field.PkgPath != "" { |
| 2546 |
panic("reflect.StructOf: field \"" + field.Name + "\" is anonymous but has PkgPath set") |
| 2547 |
} |
| 2548 |
|
| 2549 |
if field.IsExported() { |
| 2550 |
// Best-effort check for misuse. |
| 2551 |
// Since this field will be treated as exported, not much harm done if Unicode lowercase slips through. |
| 2552 |
c := field.Name[0] |
| 2553 |
if 'a' <= c && c <= 'z' || c == '_' { |
| 2554 |
panic("reflect.StructOf: field \"" + field.Name + "\" is unexported but missing PkgPath") |
| 2555 |
} |
| 2556 |
} |
| 2557 |
|
| 2558 |
resolveReflectType(field.Type.common()) // install in runtime |
| 2559 |
f := structField{ |
| 2560 |
Name: newName(field.Name, string(field.Tag), field.IsExported(), field.Anonymous), |
| 2561 |
Typ: field.Type.common(), |
| 2562 |
Offset: 0, |
| 2563 |
} |
| 2564 |
return f, field.PkgPath |
| 2565 |
} |
| 2566 |
|
| 2567 |
// typeptrdata returns the length in bytes of the prefix of t |
| 2568 |
// containing pointer data. Anything after this offset is scalar data. |
| 2569 |
// keep in sync with ../cmd/compile/internal/reflectdata/reflect.go |
| 2570 |
func typeptrdata(t *abi.Type) uintptr { |
| 2571 |
switch t.Kind() { |
| 2572 |
case abi.Struct: |
| 2573 |
st := (*structType)(unsafe.Pointer(t)) |
| 2574 |
// find the last field that has pointers. |
| 2575 |
field := -1 |
| 2576 |
for i := range st.Fields { |
| 2577 |
ft := st.Fields[i].Typ |
| 2578 |
if ft.Pointers() { |
| 2579 |
field = i |
| 2580 |
} |
| 2581 |
} |
| 2582 |
if field == -1 { |
| 2583 |
return 0 |
| 2584 |
} |
| 2585 |
f := st.Fields[field] |
| 2586 |
return f.Offset + f.Typ.PtrBytes |
| 2587 |
|
| 2588 |
default: |
| 2589 |
panic("reflect.typeptrdata: unexpected type, " + stringFor(t)) |
| 2590 |
} |
| 2591 |
} |
| 2592 |
|
| 2593 |
// ArrayOf returns the array type with the given length and element type. |
| 2594 |
// For example, if t represents int, ArrayOf(5, t) represents [5]int. |
| 2595 |
// |
| 2596 |
// If the resulting type would be larger than the available address space, |
| 2597 |
// ArrayOf panics. |
| 2598 |
func ArrayOf(length int, elem Type) Type { |
| 2599 |
if length < 0 { |
| 2600 |
panic("reflect: negative length passed to ArrayOf") |
| 2601 |
} |
| 2602 |
|
| 2603 |
typ := elem.common() |
| 2604 |
|
| 2605 |
// Look in cache. |
| 2606 |
ckey := cacheKey{Array, typ, nil, uintptr(length)} |
| 2607 |
if array, ok := lookupCache.Load(ckey); ok { |
| 2608 |
return array.(Type) |
| 2609 |
} |
| 2610 |
|
| 2611 |
// Look in known types. |
| 2612 |
s := "[" + strconv.Itoa(length) + "]" + stringFor(typ) |
| 2613 |
for _, tt := range typesByString(s) { |
| 2614 |
array := (*arrayType)(unsafe.Pointer(tt)) |
| 2615 |
if array.Elem == typ { |
| 2616 |
ti, _ := lookupCache.LoadOrStore(ckey, toRType(tt)) |
| 2617 |
return ti.(Type) |
| 2618 |
} |
| 2619 |
} |
| 2620 |
|
| 2621 |
// Make an array type. |
| 2622 |
var iarray any = [1]unsafe.Pointer{} |
| 2623 |
prototype := *(**arrayType)(unsafe.Pointer(&iarray)) |
| 2624 |
array := *prototype |
| 2625 |
array.TFlag = typ.TFlag & abi.TFlagRegularMemory |
| 2626 |
array.Str = resolveReflectName(newName(s, "", false, false)) |
| 2627 |
array.Hash = fnv1(typ.Hash, '[') |
| 2628 |
for n := uint32(length); n > 0; n >>= 8 { |
| 2629 |
array.Hash = fnv1(array.Hash, byte(n)) |
| 2630 |
} |
| 2631 |
array.Hash = fnv1(array.Hash, ']') |
| 2632 |
array.Elem = typ |
| 2633 |
array.PtrToThis = 0 |
| 2634 |
if typ.Size_ > 0 { |
| 2635 |
max := ^uintptr(0) / typ.Size_ |
| 2636 |
if uintptr(length) > max { |
| 2637 |
panic("reflect.ArrayOf: array size would exceed virtual address space") |
| 2638 |
} |
| 2639 |
} |
| 2640 |
array.Size_ = typ.Size_ * uintptr(length) |
| 2641 |
if length > 0 && typ.Pointers() { |
| 2642 |
array.PtrBytes = typ.Size_*uintptr(length-1) + typ.PtrBytes |
| 2643 |
} else { |
| 2644 |
array.PtrBytes = 0 |
| 2645 |
} |
| 2646 |
array.Align_ = typ.Align_ |
| 2647 |
array.FieldAlign_ = typ.FieldAlign_ |
| 2648 |
array.Len = uintptr(length) |
| 2649 |
array.Slice = &(SliceOf(elem).(*rtype).t) |
| 2650 |
|
| 2651 |
switch { |
| 2652 |
case array.PtrBytes == 0: |
| 2653 |
// No pointers. |
| 2654 |
array.GCData = nil |
| 2655 |
|
| 2656 |
case length == 1: |
| 2657 |
// In memory, 1-element array looks just like the element. |
| 2658 |
// We share the bitmask with the element type. |
| 2659 |
array.TFlag |= typ.TFlag & abi.TFlagGCMaskOnDemand |
| 2660 |
array.GCData = typ.GCData |
| 2661 |
|
| 2662 |
case array.PtrBytes <= abi.MaxPtrmaskBytes*8*goarch.PtrSize: |
| 2663 |
// Create pointer mask by repeating the element bitmask Len times. |
| 2664 |
n := (array.PtrBytes/goarch.PtrSize + 7) / 8 |
| 2665 |
// Runtime needs pointer masks to be a multiple of uintptr in size. |
| 2666 |
n = (n + goarch.PtrSize - 1) &^ (goarch.PtrSize - 1) |
| 2667 |
mask := make([]byte, n) |
| 2668 |
emitGCMask(mask, 0, typ, array.Len) |
| 2669 |
array.GCData = &mask[0] |
| 2670 |
|
| 2671 |
default: |
| 2672 |
// Runtime will build the mask if needed. We just need to allocate |
| 2673 |
// space to store it. |
| 2674 |
array.TFlag |= abi.TFlagGCMaskOnDemand |
| 2675 |
array.GCData = (*byte)(unsafe.Pointer(new(uintptr))) |
| 2676 |
} |
| 2677 |
|
| 2678 |
etyp := typ |
| 2679 |
esize := etyp.Size() |
| 2680 |
|
| 2681 |
array.Equal = nil |
| 2682 |
if eequal := etyp.Equal; eequal != nil { |
| 2683 |
array.Equal = func(p, q unsafe.Pointer) bool { |
| 2684 |
for i := 0; i < length; i++ { |
| 2685 |
pi := arrayAt(p, i, esize, "i < length") |
| 2686 |
qi := arrayAt(q, i, esize, "i < length") |
| 2687 |
if !eequal(pi, qi) { |
| 2688 |
return false |
| 2689 |
} |
| 2690 |
|
| 2691 |
} |
| 2692 |
return true |
| 2693 |
} |
| 2694 |
} |
| 2695 |
|
| 2696 |
switch { |
| 2697 |
case length == 1 && !typ.IfaceIndir(): |
| 2698 |
// array of 1 direct iface type can be direct |
| 2699 |
array.Kind_ |= abi.KindDirectIface |
| 2700 |
default: |
| 2701 |
array.Kind_ &^= abi.KindDirectIface |
| 2702 |
} |
| 2703 |
|
| 2704 |
ti, _ := lookupCache.LoadOrStore(ckey, toRType(&array.Type)) |
| 2705 |
return ti.(Type) |
| 2706 |
} |
| 2707 |
|
| 2708 |
func appendVarint(x []byte, v uintptr) []byte { |
| 2709 |
for ; v >= 0x80; v >>= 7 { |
| 2710 |
x = append(x, byte(v|0x80)) |
| 2711 |
} |
| 2712 |
x = append(x, byte(v)) |
| 2713 |
return x |
| 2714 |
} |
| 2715 |
|
| 2716 |
// toType converts from a *rtype to a Type that can be returned |
| 2717 |
// to the client of package reflect. In gc, the only concern is that |
| 2718 |
// a nil *rtype must be replaced by a nil Type, but in gccgo this |
| 2719 |
// function takes care of ensuring that multiple *rtype for the same |
| 2720 |
// type are coalesced into a single Type. |
| 2721 |
// |
| 2722 |
// toType should be an internal detail, |
| 2723 |
// but widely used packages access it using linkname. |
| 2724 |
// Notable members of the hall of shame include: |
| 2725 |
// - fortio.org/log |
| 2726 |
// - github.com/goccy/go-json |
| 2727 |
// - github.com/goccy/go-reflect |
| 2728 |
// - github.com/sohaha/zlsgo |
| 2729 |
// |
| 2730 |
// Do not remove or change the type signature. |
| 2731 |
// See go.dev/issue/67401. |
| 2732 |
// |
| 2733 |
//go:linkname toType |
| 2734 |
func toType(t *abi.Type) Type { |
| 2735 |
if t == nil { |
| 2736 |
return nil |
| 2737 |
} |
| 2738 |
return toRType(t) |
| 2739 |
} |
| 2740 |
|
| 2741 |
type layoutKey struct { |
| 2742 |
ftyp *funcType // function signature |
| 2743 |
rcvr *abi.Type // receiver type, or nil if none |
| 2744 |
} |
| 2745 |
|
| 2746 |
type layoutType struct { |
| 2747 |
t *abi.Type |
| 2748 |
framePool *sync.Pool |
| 2749 |
abid abiDesc |
| 2750 |
} |
| 2751 |
|
| 2752 |
var layoutCache sync.Map // map[layoutKey]layoutType |
| 2753 |
|
| 2754 |
// funcLayout computes a struct type representing the layout of the |
| 2755 |
// stack-assigned function arguments and return values for the function |
| 2756 |
// type t. |
| 2757 |
// If rcvr != nil, rcvr specifies the type of the receiver. |
| 2758 |
// The returned type exists only for GC, so we only fill out GC relevant info. |
| 2759 |
// Currently, that's just size and the GC program. We also fill in |
| 2760 |
// the name for possible debugging use. |
| 2761 |
func funcLayout(t *funcType, rcvr *abi.Type) (frametype *abi.Type, framePool *sync.Pool, abid abiDesc) { |
| 2762 |
if t.Kind() != abi.Func { |
| 2763 |
panic("reflect: funcLayout of non-func type " + stringFor(&t.Type)) |
| 2764 |
} |
| 2765 |
if rcvr != nil && rcvr.Kind() == abi.Interface { |
| 2766 |
panic("reflect: funcLayout with interface receiver " + stringFor(rcvr)) |
| 2767 |
} |
| 2768 |
k := layoutKey{t, rcvr} |
| 2769 |
if lti, ok := layoutCache.Load(k); ok { |
| 2770 |
lt := lti.(layoutType) |
| 2771 |
return lt.t, lt.framePool, lt.abid |
| 2772 |
} |
| 2773 |
|
| 2774 |
// Compute the ABI layout. |
| 2775 |
abid = newAbiDesc(t, rcvr) |
| 2776 |
|
| 2777 |
// build dummy rtype holding gc program |
| 2778 |
x := &abi.Type{ |
| 2779 |
Align_: goarch.PtrSize, |
| 2780 |
// Don't add spill space here; it's only necessary in |
| 2781 |
// reflectcall's frame, not in the allocated frame. |
| 2782 |
// TODO(mknyszek): Remove this comment when register |
| 2783 |
// spill space in the frame is no longer required. |
| 2784 |
Size_: align(abid.retOffset+abid.ret.stackBytes, goarch.PtrSize), |
| 2785 |
PtrBytes: uintptr(abid.stackPtrs.n) * goarch.PtrSize, |
| 2786 |
} |
| 2787 |
if abid.stackPtrs.n > 0 { |
| 2788 |
x.GCData = &abid.stackPtrs.data[0] |
| 2789 |
} |
| 2790 |
|
| 2791 |
var s string |
| 2792 |
if rcvr != nil { |
| 2793 |
s = "methodargs(" + stringFor(rcvr) + ")(" + stringFor(&t.Type) + ")" |
| 2794 |
} else { |
| 2795 |
s = "funcargs(" + stringFor(&t.Type) + ")" |
| 2796 |
} |
| 2797 |
x.Str = resolveReflectName(newName(s, "", false, false)) |
| 2798 |
|
| 2799 |
// cache result for future callers |
| 2800 |
framePool = &sync.Pool{New: func() any { |
| 2801 |
return unsafe_New(x) |
| 2802 |
}} |
| 2803 |
lti, _ := layoutCache.LoadOrStore(k, layoutType{ |
| 2804 |
t: x, |
| 2805 |
framePool: framePool, |
| 2806 |
abid: abid, |
| 2807 |
}) |
| 2808 |
lt := lti.(layoutType) |
| 2809 |
return lt.t, lt.framePool, lt.abid |
| 2810 |
} |
| 2811 |
|
| 2812 |
// Note: this type must agree with runtime.bitvector. |
| 2813 |
type bitVector struct { |
| 2814 |
n uint32 // number of bits |
| 2815 |
data []byte |
| 2816 |
} |
| 2817 |
|
| 2818 |
// append a bit to the bitmap. |
| 2819 |
func (bv *bitVector) append(bit uint8) { |
| 2820 |
if bv.n%(8*goarch.PtrSize) == 0 { |
| 2821 |
// Runtime needs pointer masks to be a multiple of uintptr in size. |
| 2822 |
// Since reflect passes bv.data directly to the runtime as a pointer mask, |
| 2823 |
// we append a full uintptr of zeros at a time. |
| 2824 |
for i := 0; i < goarch.PtrSize; i++ { |
| 2825 |
bv.data = append(bv.data, 0) |
| 2826 |
} |
| 2827 |
} |
| 2828 |
bv.data[bv.n/8] |= bit << (bv.n % 8) |
| 2829 |
bv.n++ |
| 2830 |
} |
| 2831 |
|
| 2832 |
func addTypeBits(bv *bitVector, offset uintptr, t *abi.Type) { |
| 2833 |
if !t.Pointers() { |
| 2834 |
return |
| 2835 |
} |
| 2836 |
|
| 2837 |
switch Kind(t.Kind_ & abi.KindMask) { |
| 2838 |
case Chan, Func, Map, Pointer, Slice, String, UnsafePointer: |
| 2839 |
// 1 pointer at start of representation |
| 2840 |
for bv.n < uint32(offset/goarch.PtrSize) { |
| 2841 |
bv.append(0) |
| 2842 |
} |
| 2843 |
bv.append(1) |
| 2844 |
|
| 2845 |
case Interface: |
| 2846 |
// 2 pointers |
| 2847 |
for bv.n < uint32(offset/goarch.PtrSize) { |
| 2848 |
bv.append(0) |
| 2849 |
} |
| 2850 |
bv.append(1) |
| 2851 |
bv.append(1) |
| 2852 |
|
| 2853 |
case Array: |
| 2854 |
// repeat inner type |
| 2855 |
tt := (*arrayType)(unsafe.Pointer(t)) |
| 2856 |
for i := 0; i < int(tt.Len); i++ { |
| 2857 |
addTypeBits(bv, offset+uintptr(i)*tt.Elem.Size_, tt.Elem) |
| 2858 |
} |
| 2859 |
|
| 2860 |
case Struct: |
| 2861 |
// apply fields |
| 2862 |
tt := (*structType)(unsafe.Pointer(t)) |
| 2863 |
for i := range tt.Fields { |
| 2864 |
f := &tt.Fields[i] |
| 2865 |
addTypeBits(bv, offset+f.Offset, f.Typ) |
| 2866 |
} |
| 2867 |
} |
| 2868 |
} |