// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package reflectlite implements lightweight version of reflect, not using // any package except for "runtime" and "unsafe".
package reflectlite import ( ) // Type is the representation of a Go type. // // Not all methods apply to all kinds of types. Restrictions, // if any, are noted in the documentation for each method. // Use the Kind method to find out the kind of type before // calling kind-specific methods. Calling a method // inappropriate to the kind of type causes a run-time panic. // // Type values are comparable, such as with the == operator, // so they can be used as map keys. // Two Type values are equal if they represent identical types. type Type interface { // Methods applicable to all types. // Name returns the type's name within its package for a defined type. // For other (non-defined) types it returns the empty string. Name() string // PkgPath returns a defined type's package path, that is, the import path // that uniquely identifies the package, such as "encoding/base64". // If the type was predeclared (string, error) or not defined (*T, struct{}, // []int, or A where A is an alias for a non-defined type), the package path // will be the empty string. PkgPath() string // Size returns the number of bytes needed to store // a value of the given type; it is analogous to unsafe.Sizeof. Size() uintptr // Kind returns the specific kind of this type. Kind() Kind // Implements reports whether the type implements the interface type u. Implements(u Type) bool // AssignableTo reports whether a value of the type is assignable to type u. AssignableTo(u Type) bool // Comparable reports whether values of this type are comparable. Comparable() bool // String returns a string representation of the type. // The string representation may use shortened package names // (e.g., base64 instead of "encoding/base64") and is not // guaranteed to be unique among types. To test for type identity, // compare the Types directly. String() string // Elem returns a type's element type. // It panics if the type's Kind is not Ptr. Elem() Type common() *rtype uncommon() *uncommonType } /* * These data structures are known to the compiler (../../cmd/internal/reflectdata/reflect.go). * A few are known to ../runtime/type.go to convey to debuggers. * They are also known to ../runtime/type.go. */ // A Kind represents the specific kind of type that a Type represents. // The zero Kind is not a valid kind. type Kind uint const ( Invalid Kind = iota Bool Int Int8 Int16 Int32 Int64 Uint Uint8 Uint16 Uint32 Uint64 Uintptr Float32 Float64 Complex64 Complex128 Array Chan Func Interface Map Pointer Slice String Struct UnsafePointer ) const Ptr = Pointer // tflag is used by an rtype to signal what extra type information is // available in the memory directly following the rtype value. // // tflag values must be kept in sync with copies in: // cmd/compile/internal/reflectdata/reflect.go // cmd/link/internal/ld/decodesym.go // runtime/type.go type tflag uint8 const ( // tflagUncommon means that there is a pointer, *uncommonType, // just beyond the outer type structure. // // For example, if t.Kind() == Struct and t.tflag&tflagUncommon != 0, // then t has uncommonType data and it can be accessed as: // // type tUncommon struct { // structType // u uncommonType // } // u := &(*tUncommon)(unsafe.Pointer(t)).u tflagUncommon tflag = 1 << 0 // tflagExtraStar means the name in the str field has an // extraneous '*' prefix. This is because for most types T in // a program, the type *T also exists and reusing the str data // saves binary size. tflagExtraStar tflag = 1 << 1 // tflagNamed means the type has a name. tflagNamed tflag = 1 << 2 // tflagRegularMemory means that equal and hash functions can treat // this type as a single region of t.size bytes. tflagRegularMemory tflag = 1 << 3 ) // rtype is the common implementation of most values. // It is embedded in other struct types. // // rtype must be kept in sync with ../runtime/type.go:/^type._type. type rtype struct { size uintptr ptrdata uintptr // number of bytes in the type that can contain pointers hash uint32 // hash of type; avoids computation in hash tables tflag tflag // extra type information flags align uint8 // alignment of variable with this type fieldAlign uint8 // alignment of struct field with this type kind uint8 // enumeration for C // function for comparing objects of this type // (ptr to object A, ptr to object B) -> ==? equal func(unsafe.Pointer, unsafe.Pointer) bool gcdata *byte // garbage collection data str nameOff // string form ptrToThis typeOff // type for pointer to this type, may be zero } // Method on non-interface type type method struct { name nameOff // name of method mtyp typeOff // method type (without receiver) ifn textOff // fn used in interface call (one-word receiver) tfn textOff // fn used for normal method call } // uncommonType is present only for defined types or types with methods // (if T is a defined type, the uncommonTypes for T and *T have methods). // Using a pointer to this struct reduces the overall size required // to describe a non-defined type with no methods. type uncommonType struct { pkgPath nameOff // import path; empty for built-in types like int, string mcount uint16 // number of methods xcount uint16 // number of exported methods moff uint32 // offset from this uncommontype to [mcount]method _ uint32 // unused } // chanDir represents a channel type's direction. type chanDir int const ( recvDir chanDir = 1 << iota // <-chan sendDir // chan<- bothDir = recvDir | sendDir // chan ) // arrayType represents a fixed array type. type arrayType struct { rtype elem *rtype // array element type slice *rtype // slice type len uintptr } // chanType represents a channel type. type chanType struct { rtype elem *rtype // channel element type dir uintptr // channel direction (chanDir) } // funcType represents a function type. // // A *rtype for each in and out parameter is stored in an array that // directly follows the funcType (and possibly its uncommonType). So // a function type with one method, one input, and one output is: // // struct { // funcType // uncommonType // [2]*rtype // [0] is in, [1] is out // } type funcType struct { rtype inCount uint16 outCount uint16 // top bit is set if last input parameter is ... } // imethod represents a method on an interface type type imethod struct { name nameOff // name of method typ typeOff // .(*FuncType) underneath } // interfaceType represents an interface type. type interfaceType struct { rtype pkgPath name // import path methods []imethod // sorted by hash } // mapType represents a map type. type mapType struct { rtype key *rtype // map key type elem *rtype // map element (value) type bucket *rtype // internal bucket structure // function for hashing keys (ptr to key, seed) -> hash hasher func(unsafe.Pointer, uintptr) uintptr keysize uint8 // size of key slot valuesize uint8 // size of value slot bucketsize uint16 // size of bucket flags uint32 } // ptrType represents a pointer type. type ptrType struct { rtype elem *rtype // pointer element (pointed at) type } // sliceType represents a slice type. type sliceType struct { rtype elem *rtype // slice element type } // Struct field type structField struct { name name // name is always non-empty typ *rtype // type of field offsetEmbed uintptr // byte offset of field<<1 | isEmbedded } func ( *structField) () uintptr { return .offsetEmbed >> 1 } func ( *structField) () bool { return .offsetEmbed&1 != 0 } // structType represents a struct type. type structType struct { rtype pkgPath name fields []structField // sorted by offset } // name is an encoded type name with optional extra data. // // The first byte is a bit field containing: // // 1<<0 the name is exported // 1<<1 tag data follows the name // 1<<2 pkgPath nameOff follows the name and tag // // The next two bytes are the data length: // // l := uint16(data[1])<<8 | uint16(data[2]) // // Bytes [3:3+l] are the string data. // // If tag data follows then bytes 3+l and 3+l+1 are the tag length, // with the data following. // // If the import path follows, then 4 bytes at the end of // the data form a nameOff. The import path is only set for concrete // methods that are defined in a different package than their type. // // If a name starts with "*", then the exported bit represents // whether the pointed to type is exported. type name struct { bytes *byte } func ( name) ( int, string) *byte { return (*byte)(add(unsafe.Pointer(.bytes), uintptr(), )) } func ( name) () bool { return (*.bytes)&(1<<0) != 0 } func ( name) () bool { return (*.bytes)&(1<<1) != 0 } // readVarint parses a varint as encoded by encoding/binary. // It returns the number of encoded bytes and the encoded value. func ( name) ( int) (int, int) { := 0 for := 0; ; ++ { := *.data(+, "read varint") += int(&0x7f) << (7 * ) if &0x80 == 0 { return + 1, } } } func ( name) () ( string) { if .bytes == nil { return } , := .readVarint(1) := (*unsafeheader.String)(unsafe.Pointer(&)) .Data = unsafe.Pointer(.data(1+, "non-empty string")) .Len = return } func ( name) () ( string) { if !.hasTag() { return "" } , := .readVarint(1) , := .readVarint(1 + + ) := (*unsafeheader.String)(unsafe.Pointer(&)) .Data = unsafe.Pointer(.data(1+++, "non-empty string")) .Len = return } func ( name) () string { if .bytes == nil || *.data(0, "name flag field")&(1<<2) == 0 { return "" } , := .readVarint(1) := 1 + + if .hasTag() { , := .readVarint() += + } var int32 // Note that this field may not be aligned in memory, // so we cannot use a direct int32 assignment here. copy((*[4]byte)(unsafe.Pointer(&))[:], (*[4]byte)(unsafe.Pointer(.data(, "name offset field")))[:]) := name{(*byte)(resolveTypeOff(unsafe.Pointer(.bytes), ))} return .name() } /* * The compiler knows the exact layout of all the data structures above. * The compiler does not know about the data structures and methods below. */ const ( kindDirectIface = 1 << 5 kindGCProg = 1 << 6 // Type.gc points to GC program kindMask = (1 << 5) - 1 ) // String returns the name of k. func ( Kind) () string { if int() < len(kindNames) { return kindNames[] } return kindNames[0] } var kindNames = []string{ Invalid: "invalid", Bool: "bool", Int: "int", Int8: "int8", Int16: "int16", Int32: "int32", Int64: "int64", Uint: "uint", Uint8: "uint8", Uint16: "uint16", Uint32: "uint32", Uint64: "uint64", Uintptr: "uintptr", Float32: "float32", Float64: "float64", Complex64: "complex64", Complex128: "complex128", Array: "array", Chan: "chan", Func: "func", Interface: "interface", Map: "map", Ptr: "ptr", Slice: "slice", String: "string", Struct: "struct", UnsafePointer: "unsafe.Pointer", } func ( *uncommonType) () []method { if .mcount == 0 { return nil } return (*[1 << 16]method)(add(unsafe.Pointer(), uintptr(.moff), "t.mcount > 0"))[:.mcount:.mcount] } func ( *uncommonType) () []method { if .xcount == 0 { return nil } return (*[1 << 16]method)(add(unsafe.Pointer(), uintptr(.moff), "t.xcount > 0"))[:.xcount:.xcount] } // resolveNameOff resolves a name offset from a base pointer. // The (*rtype).nameOff method is a convenience wrapper for this function. // Implemented in the runtime package. func ( unsafe.Pointer, int32) unsafe.Pointer // resolveTypeOff resolves an *rtype offset from a base type. // The (*rtype).typeOff method is a convenience wrapper for this function. // Implemented in the runtime package. func ( unsafe.Pointer, int32) unsafe.Pointer type nameOff int32 // offset to a name type typeOff int32 // offset to an *rtype type textOff int32 // offset from top of text section func ( *rtype) ( nameOff) name { return name{(*byte)(resolveNameOff(unsafe.Pointer(), int32()))} } func ( *rtype) ( typeOff) *rtype { return (*rtype)(resolveTypeOff(unsafe.Pointer(), int32())) } func ( *rtype) () *uncommonType { if .tflag&tflagUncommon == 0 { return nil } switch .Kind() { case Struct: return &(*structTypeUncommon)(unsafe.Pointer()).u case Ptr: type struct { ptrType uncommonType } return &(*)(unsafe.Pointer()). case Func: type struct { funcType uncommonType } return &(*)(unsafe.Pointer()). case Slice: type struct { sliceType uncommonType } return &(*)(unsafe.Pointer()). case Array: type struct { arrayType uncommonType } return &(*)(unsafe.Pointer()). case Chan: type struct { chanType uncommonType } return &(*)(unsafe.Pointer()). case Map: type struct { mapType uncommonType } return &(*)(unsafe.Pointer()). case Interface: type struct { interfaceType uncommonType } return &(*)(unsafe.Pointer()). default: type struct { rtype uncommonType } return &(*)(unsafe.Pointer()). } } func ( *rtype) () string { := .nameOff(.str).name() if .tflag&tflagExtraStar != 0 { return [1:] } return } func ( *rtype) () uintptr { return .size } func ( *rtype) () Kind { return Kind(.kind & kindMask) } func ( *rtype) () bool { return .ptrdata != 0 } func ( *rtype) () *rtype { return } func ( *rtype) () []method { := .uncommon() if == nil { return nil } return .exportedMethods() } func ( *rtype) () int { if .Kind() == Interface { := (*interfaceType)(unsafe.Pointer()) return .NumMethod() } return len(.exportedMethods()) } func ( *rtype) () string { if .tflag&tflagNamed == 0 { return "" } := .uncommon() if == nil { return "" } return .nameOff(.pkgPath).name() } func ( *rtype) () bool { return .tflag&tflagNamed != 0 } func ( *rtype) () string { if !.hasName() { return "" } := .String() := len() - 1 for >= 0 && [] != '.' { -- } return [+1:] } func ( *rtype) () chanDir { if .Kind() != Chan { panic("reflect: chanDir of non-chan type") } := (*chanType)(unsafe.Pointer()) return chanDir(.dir) } func ( *rtype) () Type { switch .Kind() { case Array: := (*arrayType)(unsafe.Pointer()) return toType(.elem) case Chan: := (*chanType)(unsafe.Pointer()) return toType(.elem) case Map: := (*mapType)(unsafe.Pointer()) return toType(.elem) case Ptr: := (*ptrType)(unsafe.Pointer()) return toType(.elem) case Slice: := (*sliceType)(unsafe.Pointer()) return toType(.elem) } panic("reflect: Elem of invalid type") } func ( *rtype) ( int) Type { if .Kind() != Func { panic("reflect: In of non-func type") } := (*funcType)(unsafe.Pointer()) return toType(.in()[]) } func ( *rtype) () Type { if .Kind() != Map { panic("reflect: Key of non-map type") } := (*mapType)(unsafe.Pointer()) return toType(.key) } func ( *rtype) () int { if .Kind() != Array { panic("reflect: Len of non-array type") } := (*arrayType)(unsafe.Pointer()) return int(.len) } func ( *rtype) () int { if .Kind() != Struct { panic("reflect: NumField of non-struct type") } := (*structType)(unsafe.Pointer()) return len(.fields) } func ( *rtype) () int { if .Kind() != Func { panic("reflect: NumIn of non-func type") } := (*funcType)(unsafe.Pointer()) return int(.inCount) } func ( *rtype) () int { if .Kind() != Func { panic("reflect: NumOut of non-func type") } := (*funcType)(unsafe.Pointer()) return len(.out()) } func ( *rtype) ( int) Type { if .Kind() != Func { panic("reflect: Out of non-func type") } := (*funcType)(unsafe.Pointer()) return toType(.out()[]) } func ( *funcType) () []*rtype { := unsafe.Sizeof(*) if .tflag&tflagUncommon != 0 { += unsafe.Sizeof(uncommonType{}) } if .inCount == 0 { return nil } return (*[1 << 20]*rtype)(add(unsafe.Pointer(), , "t.inCount > 0"))[:.inCount:.inCount] } func ( *funcType) () []*rtype { := unsafe.Sizeof(*) if .tflag&tflagUncommon != 0 { += unsafe.Sizeof(uncommonType{}) } := .outCount & (1<<15 - 1) if == 0 { return nil } return (*[1 << 20]*rtype)(add(unsafe.Pointer(), , "outCount > 0"))[.inCount : .inCount+ : .inCount+] } // add returns p+x. // // The whySafe string is ignored, so that the function still inlines // as efficiently as p+x, but all call sites should use the string to // record why the addition is safe, which is to say why the addition // does not cause x to advance to the very end of p's allocation // and therefore point incorrectly at the next block in memory. func ( unsafe.Pointer, uintptr, string) unsafe.Pointer { return unsafe.Pointer(uintptr() + ) } // NumMethod returns the number of interface methods in the type's method set. func ( *interfaceType) () int { return len(.methods) } // TypeOf returns the reflection Type that represents the dynamic type of i. // If i is a nil interface value, TypeOf returns nil. func ( any) Type { := *(*emptyInterface)(unsafe.Pointer(&)) return toType(.typ) } func ( *rtype) ( Type) bool { if == nil { panic("reflect: nil type passed to Type.Implements") } if .Kind() != Interface { panic("reflect: non-interface type passed to Type.Implements") } return implements(.(*rtype), ) } func ( *rtype) ( Type) bool { if == nil { panic("reflect: nil type passed to Type.AssignableTo") } := .(*rtype) return directlyAssignable(, ) || implements(, ) } func ( *rtype) () bool { return .equal != nil } // implements reports whether the type V implements the interface type T. func (, *rtype) bool { if .Kind() != Interface { return false } := (*interfaceType)(unsafe.Pointer()) if len(.methods) == 0 { return true } // The same algorithm applies in both cases, but the // method tables for an interface type and a concrete type // are different, so the code is duplicated. // In both cases the algorithm is a linear scan over the two // lists - T's methods and V's methods - simultaneously. // Since method tables are stored in a unique sorted order // (alphabetical, with no duplicate method names), the scan // through V's methods must hit a match for each of T's // methods along the way, or else V does not implement T. // This lets us run the scan in overall linear time instead of // the quadratic time a naive search would require. // See also ../runtime/iface.go. if .Kind() == Interface { := (*interfaceType)(unsafe.Pointer()) := 0 for := 0; < len(.methods); ++ { := &.methods[] := .nameOff(.name) := &.methods[] := .nameOff(.name) if .name() == .name() && .typeOff(.typ) == .typeOff(.typ) { if !.isExported() { := .pkgPath() if == "" { = .pkgPath.name() } := .pkgPath() if == "" { = .pkgPath.name() } if != { continue } } if ++; >= len(.methods) { return true } } } return false } := .uncommon() if == nil { return false } := 0 := .methods() for := 0; < int(.mcount); ++ { := &.methods[] := .nameOff(.name) := [] := .nameOff(.name) if .name() == .name() && .typeOff(.mtyp) == .typeOff(.typ) { if !.isExported() { := .pkgPath() if == "" { = .pkgPath.name() } := .pkgPath() if == "" { = .nameOff(.pkgPath).name() } if != { continue } } if ++; >= len(.methods) { return true } } } return false } // directlyAssignable reports whether a value x of type V can be directly // assigned (using memmove) to a value of type T. // https://golang.org/doc/go_spec.html#Assignability // Ignoring the interface rules (implemented elsewhere) // and the ideal constant rules (no ideal constants at run time). func (, *rtype) bool { // x's type V is identical to T? if == { return true } // Otherwise at least one of T and V must not be defined // and they must have the same kind. if .hasName() && .hasName() || .Kind() != .Kind() { return false } // x's type T and V must have identical underlying types. return haveIdenticalUnderlyingType(, , true) } func (, Type, bool) bool { if { return == } if .Name() != .Name() || .Kind() != .Kind() { return false } return haveIdenticalUnderlyingType(.common(), .common(), false) } func (, *rtype, bool) bool { if == { return true } := .Kind() if != .Kind() { return false } // Non-composite types of equal kind have same underlying type // (the predefined instance of the type). if Bool <= && <= Complex128 || == String || == UnsafePointer { return true } // Composite types. switch { case Array: return .Len() == .Len() && haveIdenticalType(.Elem(), .Elem(), ) case Chan: // Special case: // x is a bidirectional channel value, T is a channel type, // and x's type V and T have identical element types. if .chanDir() == bothDir && haveIdenticalType(.Elem(), .Elem(), ) { return true } // Otherwise continue test for identical underlying type. return .chanDir() == .chanDir() && haveIdenticalType(.Elem(), .Elem(), ) case Func: := (*funcType)(unsafe.Pointer()) := (*funcType)(unsafe.Pointer()) if .outCount != .outCount || .inCount != .inCount { return false } for := 0; < .NumIn(); ++ { if !haveIdenticalType(.In(), .In(), ) { return false } } for := 0; < .NumOut(); ++ { if !haveIdenticalType(.Out(), .Out(), ) { return false } } return true case Interface: := (*interfaceType)(unsafe.Pointer()) := (*interfaceType)(unsafe.Pointer()) if len(.methods) == 0 && len(.methods) == 0 { return true } // Might have the same methods but still // need a run time conversion. return false case Map: return haveIdenticalType(.Key(), .Key(), ) && haveIdenticalType(.Elem(), .Elem(), ) case Ptr, Slice: return haveIdenticalType(.Elem(), .Elem(), ) case Struct: := (*structType)(unsafe.Pointer()) := (*structType)(unsafe.Pointer()) if len(.fields) != len(.fields) { return false } if .pkgPath.name() != .pkgPath.name() { return false } for := range .fields { := &.fields[] := &.fields[] if .name.name() != .name.name() { return false } if !haveIdenticalType(.typ, .typ, ) { return false } if && .name.tag() != .name.tag() { return false } if .offsetEmbed != .offsetEmbed { return false } } return true } return false } type structTypeUncommon struct { structType u uncommonType } // toType converts from a *rtype to a Type that can be returned // to the client of package reflect. In gc, the only concern is that // a nil *rtype must be replaced by a nil Type, but in gccgo this // function takes care of ensuring that multiple *rtype for the same // type are coalesced into a single Type. func ( *rtype) Type { if == nil { return nil } return } // ifaceIndir reports whether t is stored indirectly in an interface value. func ( *rtype) bool { return .kind&kindDirectIface == 0 }