Source File
type.go
Belonging Package
internal/reflectlite
package reflectlite
import (
)
Name() string
PkgPath() string
Size() uintptr
Kind() Kind
Comparable() bool
String() string
Elem() Type
common() *rtype
uncommon() *uncommonType
}
tflagUncommon tflag = 1 << 0
tflagExtraStar tflag = 1 << 1
tflagNamed tflag = 1 << 2
tflagRegularMemory tflag = 1 << 3
)
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
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
}
type structType struct {
rtype
pkgPath name
fields []structField // sorted by offset
}
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) () int {
return int(uint16(*.data(1, "name len field"))<<8 | uint16(*.data(2, "name len field")))
}
func ( name) () int {
if *.data(0, "name flag field")&(1<<1) == 0 {
return 0
}
:= 3 + .nameLen()
return int(uint16(*.data(, "name taglen field"))<<8 | uint16(*.data(+1, "name taglen field")))
}
func ( name) () ( string) {
if .bytes == nil {
return
}
:= (*[4]byte)(unsafe.Pointer(.bytes))
:= (*unsafeheader.String)(unsafe.Pointer(&))
.Data = unsafe.Pointer(&[3])
.Len = int([1])<<8 | int([2])
return
}
func ( name) () ( string) {
:= .tagLen()
if == 0 {
return ""
}
:= .nameLen()
:= (*unsafeheader.String)(unsafe.Pointer(&))
.Data = unsafe.Pointer(.data(3++2, "non-empty string"))
.Len =
return
}
func ( name) () string {
if .bytes == nil || *.data(0, "name flag field")&(1<<2) == 0 {
return ""
}
:= 3 + .nameLen()
if := .tagLen(); > 0 {
+= 2 +
}
const (
kindDirectIface = 1 << 5
kindGCProg = 1 << 6 // Type.gc points to GC program
kindMask = (1 << 5) - 1
)
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]
}
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+]
}
func ( *interfaceType) () int { return len(.methods) }
func ( interface{}) 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
}
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
}
if == {
return true
}
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
}
if Bool <= && <= Complex128 || == String || == UnsafePointer {
return true
}
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
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
}
func ( *rtype) bool {
return .kind&kindDirectIface == 0
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin 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 @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |