-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.go
134 lines (119 loc) · 2.82 KB
/
type.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package safer
import (
"reflect"
"unsafe"
)
const (
// Size of ptr
ptrSize = unsafe.Sizeof(uintptr(0))
// From: runtime/typekind.go
kindBool = 1 + iota
kindInt
kindInt8
kindInt16
kindInt32
kindInt64
kindUint
kindUint8
kindUint16
kindUint32
kindUint64
kindUintptr
kindFloat32
kindFloat64
kindComplex64
kindComplex128
kindArray
kindChan
kindFunc
kindInterface
kindMap
kindPtr
kindSlice
kindString
kindStruct
kindUnsafePointer
kindDirectIface = 1 << 5
kindGCProg = 1 << 6
kindNoPointers = 1 << 7
kindMask = (1 << 5) - 1
)
func reflectKindOf(k uint8) reflect.Kind {
return reflect.Kind(k & kindMask)
}
// typeOf returns the runtimes representation of a Go type.
func typeOf(v interface{}) *_type {
return *(**_type)(unsafe.Pointer(&v))
}
// pcForFunc returns a uintptr to the given funcs pc
func pcForFunc(fn interface{}) uintptr {
if KindOf(fn) != reflect.Func {
return 0
}
return **(**uintptr)(add(unsafe.Pointer(&fn), ptrSize))
}
// From: runtime/stubs.go
// --
// Should be a built-in for unsafe.Pointer?
//go:nosplit
func add(p unsafe.Pointer, x uintptr) unsafe.Pointer {
return unsafe.Pointer(uintptr(p) + x)
}
// From: runtime/type.go
// --
// tflag is documented in reflect/type.go.
//
// tflag values must be kept in sync with copies in:
// cmd/compile/internal/gc/reflect.go
// cmd/link/internal/ld/decodesym.go
// reflect/type.go
type tflag uint8
const (
tflagUncommon tflag = 1 << 0
tflagExtraStar tflag = 1 << 1
tflagNamed tflag = 1 << 2
)
// From: runtime/alg.go
// --
// typeAlg is also copied/used in reflect/type.go.
// keep them in sync.
type typeAlg struct {
// function for hashing objects of this type
// (ptr to object, seed) -> hash
hash func(unsafe.Pointer, uintptr) uintptr
// function for comparing objects of this type
// (ptr to object A, ptr to object B) -> ==?
equal func(unsafe.Pointer, unsafe.Pointer) bool
}
// Type is a Go type.
//
// From: runtime/type.go
// --
// Needs to be in sync with ../cmd/compile/internal/ld/decodesym.go:/^func.commonsize,
// ../cmd/compile/internal/gc/reflect.go:/^func.dcommontype and
// ../reflect/type.go:/^type.rtype.
type _type struct {
size uintptr
ptrdata uintptr // size of memory prefix holding all pointers
hash uint32
tflag uint8
align uint8
fieldalign uint8
kind uint8
alg *typeAlg
// gcdata stores the GC type data for the garbage collector.
// If the KindGCProg bit is set in kind, gcdata is a GC program.
// Otherwise it is a ptrmask bitmap. See mbitmap.go for details.
gcdata *byte
str int32 // nameOff
ptrToThis int32
}
type nameOff int32
type typeOff int32
type textOff int32
// From: runtime/typekind.go
// --
// isDirectIface reports whether t is stored directly in an interface value.
// func isDirectIface(k uint8) bool {
// return k&kindDirectIface != 0
// }