// 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", "unsafe", and "internal/abi"
package reflectliteimport ()// 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.typeTypeinterface {// 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() *abi.Type 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.typeKind = abi.KindconstPtr = abi.Pointerconst (// Import-and-export these constants as necessaryInterface = abi.InterfaceSlice = abi.SliceString = abi.StringStruct = abi.Struct)type nameOff = abi.NameOfftype typeOff = abi.TypeOfftype textOff = abi.TextOfftype rtype struct { *abi.Type}// 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 = abi.UncommonType// arrayType represents a fixed array type.type arrayType = abi.ArrayType// chanType represents a channel type.type chanType = abi.ChanTypetype funcType = abi.FuncTypetype interfaceType = abi.InterfaceType// ptrType represents a pointer type.type ptrType = abi.PtrType// sliceType represents a slice type.type sliceType = abi.SliceType// structType represents a struct type.type structType = abi.StructType// 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}func ( name) () bool {return (*.bytes)&(1<<3) != 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) { := 0for := 0; ; ++ { := *.data(+, "read varint") += int(&0x7f) << (7 * )if &0x80 == 0 {return + 1, } }}func ( name) () string {if .bytes == nil {return"" } , := .readVarint(1)returnunsafe.String(.data(1+, "non-empty string"), )}func ( name) () string {if !.hasTag() {return"" } , := .readVarint(1) , := .readVarint(1 + + )returnunsafe.String(.data(1+++, "non-empty string"), )}func pkgPath( abi.Name) string {if .Bytes == nil || *.DataChecked(0, "name flag field")&(1<<2) == 0 {return"" } , := .ReadVarint(1) := 1 + + if .HasTag() { , := .ReadVarint() += + }varint32// 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(.DataChecked(, "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. */// 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.////go:noescapefunc resolveNameOff( 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.////go:noescapefunc resolveTypeOff( unsafe.Pointer, int32) unsafe.Pointerfunc ( rtype) ( nameOff) abi.Name {returnabi.Name{Bytes: (*byte)(resolveNameOff(unsafe.Pointer(.Type), int32()))}}func ( rtype) ( typeOff) *abi.Type {return (*abi.Type)(resolveTypeOff(unsafe.Pointer(.Type), int32()))}func ( rtype) () *uncommonType {return .Uncommon()}func ( rtype) () string { := .nameOff(.Str).Name()if .TFlag&abi.TFlagExtraStar != 0 {return [1:] }return}func ( rtype) () *abi.Type { return .Type }func ( rtype) () []abi.Method { := .uncommon()if == nil {returnnil }return .ExportedMethods()}func ( rtype) () int { := .Type.InterfaceType()if != nil {return .NumMethod() }returnlen(.exportedMethods())}func ( rtype) () string {if .TFlag&abi.TFlagNamed == 0 {return"" } := .uncommon()if == nil {return"" }return .nameOff(.PkgPath).Name()}func ( rtype) () string {if !.HasName() {return"" } := .String() := len() - 1 := 0for >= 0 && ([] != '.' || != 0) {switch [] {case']': ++case'[': -- } -- }return [+1:]}func toRType( *abi.Type) rtype {returnrtype{}}func elem( *abi.Type) *abi.Type { := .Elem()if != nil {return }panic("reflect: Elem of invalid type " + toRType().String())}func ( rtype) () Type {returntoType(elem(.common()))}func ( rtype) ( int) Type { := .Type.FuncType()if == nil {panic("reflect: In of non-func type") }returntoType(.InSlice()[])}func ( rtype) () Type { := .Type.MapType()if == nil {panic("reflect: Key of non-map type") }returntoType(.Key)}func ( rtype) () int { := .Type.ArrayType()if == nil {panic("reflect: Len of non-array type") }returnint(.Len)}func ( rtype) () int { := .Type.StructType()if == nil {panic("reflect: NumField of non-struct type") }returnlen(.Fields)}func ( rtype) () int { := .Type.FuncType()if == nil {panic("reflect: NumIn of non-func type") }returnint(.InCount)}func ( rtype) () int { := .Type.FuncType()if == nil {panic("reflect: NumOut of non-func type") }return .NumOut()}func ( rtype) ( int) Type { := .Type.FuncType()if == nil {panic("reflect: Out of non-func type") }returntoType(.OutSlice()[])}// 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 add( unsafe.Pointer, uintptr, string) unsafe.Pointer {returnunsafe.Pointer(uintptr() + )}// 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 {returntoType(abi.TypeOf())}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") }returnimplements(.common(), .common())}func ( rtype) ( Type) bool {if == nil {panic("reflect: nil type passed to Type.AssignableTo") } := .common() := .common()returndirectlyAssignable(, ) || implements(, )}func ( rtype) () bool {return .Equal != nil}// implements reports whether the type V implements the interface type T.func implements(, *abi.Type) bool { := .InterfaceType()if == nil {returnfalse }iflen(.Methods) == 0 {returntrue } := toRType() := toRType()// 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()) := 0for := 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) {returntrue } } }returnfalse } := .Uncommon()if == nil {returnfalse } := 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) {returntrue } } }returnfalse}// 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 directlyAssignable(, *abi.Type) bool {// x's type V is identical to T?if == {returntrue }// Otherwise at least one of T and V must not be defined // and they must have the same kind.if .HasName() && .HasName() || .Kind() != .Kind() {returnfalse }// x's type T and V must have identical underlying types.returnhaveIdenticalUnderlyingType(, , true)}func haveIdenticalType(, *abi.Type, bool) bool {if {return == }iftoRType().Name() != toRType().Name() || .Kind() != .Kind() {returnfalse }returnhaveIdenticalUnderlyingType(, , false)}func haveIdenticalUnderlyingType(, *abi.Type, bool) bool {if == {returntrue } := .Kind()if != .Kind() {returnfalse }// Non-composite types of equal kind have same underlying type // (the predefined instance of the type).ifabi.Bool <= && <= abi.Complex128 || == abi.String || == abi.UnsafePointer {returntrue }// Composite types.switch {caseabi.Array:return .Len() == .Len() && haveIdenticalType(.Elem(), .Elem(), )caseabi.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() == abi.BothDir && haveIdenticalType(.Elem(), .Elem(), ) {returntrue }// Otherwise continue test for identical underlying type.return .ChanDir() == .ChanDir() && haveIdenticalType(.Elem(), .Elem(), )caseabi.Func: := (*funcType)(unsafe.Pointer()) := (*funcType)(unsafe.Pointer())if .OutCount != .OutCount || .InCount != .InCount {returnfalse }for := 0; < .NumIn(); ++ {if !haveIdenticalType(.In(), .In(), ) {returnfalse } }for := 0; < .NumOut(); ++ {if !haveIdenticalType(.Out(), .Out(), ) {returnfalse } }returntruecaseInterface: := (*interfaceType)(unsafe.Pointer()) := (*interfaceType)(unsafe.Pointer())iflen(.Methods) == 0 && len(.Methods) == 0 {returntrue }// Might have the same methods but still // need a run time conversion.returnfalsecaseabi.Map:returnhaveIdenticalType(.Key(), .Key(), ) && haveIdenticalType(.Elem(), .Elem(), )casePtr, abi.Slice:returnhaveIdenticalType(.Elem(), .Elem(), )caseabi.Struct: := (*structType)(unsafe.Pointer()) := (*structType)(unsafe.Pointer())iflen(.Fields) != len(.Fields) {returnfalse }if .PkgPath.Name() != .PkgPath.Name() {returnfalse }for := range .Fields { := &.Fields[] := &.Fields[]if .Name.Name() != .Name.Name() {returnfalse }if !haveIdenticalType(.Typ, .Typ, ) {returnfalse }if && .Name.Tag() != .Name.Tag() {returnfalse }if .Offset != .Offset {returnfalse }if .Embedded() != .Embedded() {returnfalse } }returntrue }returnfalse}// 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 toType( *abi.Type) Type {if == nil {returnnil }returntoRType()}
The pages are generated with Goldsv0.7.0-preview. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.