-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrtype.go
567 lines (524 loc) · 15.6 KB
/
rtype.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
//go:build !js || (js && wasm)
// +build !js js,wasm
package reflectx
import (
"fmt"
"io"
"reflect"
"unsafe"
)
func toStructType(t *rtype) *structType {
return (*structType)(unsafe.Pointer(t))
}
func toKindType(t *rtype) unsafe.Pointer {
return unsafe.Pointer(t)
}
//go:linkname toUncommonType reflect.(*rtype).uncommon
func toUncommonType(t *rtype) *uncommonType
// 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
}
type funcTypeFixed1 struct {
funcType
args [1]*rtype
}
type funcTypeFixed4 struct {
funcType
args [4]*rtype
}
type funcTypeFixed8 struct {
funcType
args [8]*rtype
}
type funcTypeFixed16 struct {
funcType
args [16]*rtype
}
type funcTypeFixed32 struct {
funcType
args [32]*rtype
}
type funcTypeFixed64 struct {
funcType
args [64]*rtype
}
type funcTypeFixed128 struct {
funcType
args [128]*rtype
}
// emptyInterface is the header for an interface{} value.
type emptyInterface struct {
typ *rtype
word unsafe.Pointer
}
func totype(typ reflect.Type) *rtype {
e := (*emptyInterface)(unsafe.Pointer(&typ))
return (*rtype)(e.word)
}
//go:nocheckptr
func (t *uncommonType) methods() []method {
if t == nil || t.mcount == 0 {
return nil
}
return (*[1 << 16]method)(add(unsafe.Pointer(t), uintptr(t.moff), "t.mcount > 0"))[:t.mcount:t.mcount]
}
//go:nocheckptr
func (t *uncommonType) exportedMethods() []method {
if t == nil || t.xcount == 0 {
return nil
}
return (*[1 << 16]method)(add(unsafe.Pointer(t), uintptr(t.moff), "t.xcount > 0"))[:t.xcount:t.xcount]
}
func tovalue(v *reflect.Value) *Value {
return (*Value)(unsafe.Pointer(v))
}
func toValue(v Value) reflect.Value {
return *(*reflect.Value)(unsafe.Pointer(&v))
}
func (t *rtype) uncommon() *uncommonType {
return toUncommonType(t)
}
func (t *rtype) exportedMethods() []method {
ut := t.uncommon()
if ut == nil {
return nil
}
return ut.exportedMethods()
}
func (t *rtype) methods() []method {
ut := t.uncommon()
if ut == nil {
return nil
}
return ut.methods()
}
func (t *funcType) in() []*rtype {
uadd := unsafe.Sizeof(*t)
if t.tflag&tflagUncommon != 0 {
uadd += unsafe.Sizeof(uncommonType{})
}
if t.inCount == 0 {
return nil
}
return (*[1 << 20]*rtype)(add(unsafe.Pointer(t), uadd, "t.inCount > 0"))[:t.inCount:t.inCount]
}
func (t *funcType) out() []*rtype {
uadd := unsafe.Sizeof(*t)
if t.tflag&tflagUncommon != 0 {
uadd += unsafe.Sizeof(uncommonType{})
}
outCount := t.outCount & (1<<15 - 1)
if outCount == 0 {
return nil
}
return (*[1 << 20]*rtype)(add(unsafe.Pointer(t), uadd, "outCount > 0"))[t.inCount : t.inCount+outCount : t.inCount+outCount]
}
func (t *rtype) IsVariadic() bool {
if t.Kind() != reflect.Func {
panic("reflect: IsVariadic of non-func type " + toType(t).String())
}
tt := (*funcType)(unsafe.Pointer(t))
return tt.outCount&(1<<15) != 0
}
type bitVector struct {
n uint32 // number of bits
data []byte
}
// 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 ...
}
type uncommonFuncType struct {
funcType
uncommonType
args [1]*rtype
}
func uncommonFuncTypeArgs(rt *rtype, nargs int) []*rtype {
f := (*uncommonFuncType)(unsafe.Pointer(rt))
return (*[1 << 16]*rtype)(unsafe.Pointer(&f.args))[:nargs:nargs]
}
func SetUnderlying(typ reflect.Type, styp reflect.Type) {
rt := totype(typ)
ort := totype(styp)
switch styp.Kind() {
case reflect.Struct:
st := (*structType)(unsafe.Pointer(rt))
ost := (*structType)(unsafe.Pointer(ort))
st.fields = ost.fields
case reflect.Ptr:
st := (*ptrType)(unsafe.Pointer(rt))
ost := (*ptrType)(unsafe.Pointer(ort))
st.elem = ost.elem
case reflect.Slice:
st := (*sliceType)(unsafe.Pointer(rt))
ost := (*sliceType)(unsafe.Pointer(ort))
st.elem = ost.elem
case reflect.Array:
st := (*arrayType)(unsafe.Pointer(rt))
ost := (*arrayType)(unsafe.Pointer(ort))
st.elem = ost.elem
st.slice = ost.slice
st.len = ost.len
case reflect.Chan:
st := (*chanType)(unsafe.Pointer(rt))
ost := (*chanType)(unsafe.Pointer(ort))
st.elem = ost.elem
st.dir = ost.dir
case reflect.Interface:
st := (*interfaceType)(unsafe.Pointer(rt))
ost := (*interfaceType)(unsafe.Pointer(ort))
st.methods = ost.methods
case reflect.Map:
st := (*mapType)(unsafe.Pointer(rt))
ost := (*mapType)(unsafe.Pointer(ort))
st.key = ost.key
st.elem = ost.elem
st.bucket = ost.bucket
st.hasher = ost.hasher
st.keysize = ost.keysize
st.valuesize = ost.valuesize
st.bucketsize = ost.bucketsize
st.flags = ost.flags
case reflect.Func:
st := (*funcType)(unsafe.Pointer(rt))
ost := (*funcType)(unsafe.Pointer(ort))
st.inCount = ost.inCount
st.outCount = ost.outCount
numIn := typ.NumIn()
numOut := typ.NumOut()
narg := numIn + numOut
if narg > 0 {
args := uncommonFuncTypeArgs(rt, narg)
var i int
for i = 0; i < numIn; i++ {
args[i] = totype(styp.In(i))
}
for j := 0; j < numOut; j++ {
args[i+j] = totype(styp.Out(j))
}
}
}
rt.size = ort.size
rt.tflag |= tflagUncommon | tflagExtraStar | tflagNamed
rt.kind = ort.kind
rt.align = ort.align
rt.fieldAlign = ort.fieldAlign
rt.gcdata = ort.gcdata
rt.ptrdata = ort.ptrdata
rt.equal = ort.equal
//rt.str = resolveReflectName(ort.nameOff(ort.str))
if isRegularMemory(typ) {
rt.tflag |= tflagRegularMemory
}
}
func newType(pkg string, name string, styp reflect.Type, mcount int, xcount int) (*rtype, []method) {
var rt *rtype
var fnoff uint32
var tt reflect.Value
ort := totype(styp)
skind := styp.Kind()
switch skind {
case reflect.Struct:
tt = reflect.New(reflect.StructOf([]reflect.StructField{
{Name: "S", Type: reflect.TypeOf(structType{})},
{Name: "U", Type: reflect.TypeOf(uncommonType{})},
{Name: "M", Type: reflect.ArrayOf(mcount, reflect.TypeOf(method{}))},
}))
st := (*structType)(unsafe.Pointer(tt.Elem().Field(0).UnsafeAddr()))
ost := (*structType)(unsafe.Pointer(ort))
st.fields = ost.fields
case reflect.Ptr:
tt = reflect.New(reflect.StructOf([]reflect.StructField{
{Name: "S", Type: reflect.TypeOf(ptrType{})},
{Name: "U", Type: reflect.TypeOf(uncommonType{})},
{Name: "M", Type: reflect.ArrayOf(mcount, reflect.TypeOf(method{}))},
}))
st := (*ptrType)(unsafe.Pointer(tt.Elem().Field(0).UnsafeAddr()))
st.elem = totype(styp.Elem())
case reflect.Interface:
tt = reflect.New(reflect.StructOf([]reflect.StructField{
{Name: "S", Type: reflect.TypeOf(interfaceType{})},
{Name: "U", Type: reflect.TypeOf(uncommonType{})},
}))
st := (*interfaceType)(unsafe.Pointer(tt.Elem().Field(0).UnsafeAddr()))
ost := (*interfaceType)(unsafe.Pointer(ort))
for _, m := range ost.methods {
st.methods = append(st.methods, imethod{
name: resolveReflectName(ost.nameOff(m.name)),
typ: resolveReflectType(ost.typeOff(m.typ)),
})
}
case reflect.Slice:
tt = reflect.New(reflect.StructOf([]reflect.StructField{
{Name: "S", Type: reflect.TypeOf(sliceType{})},
{Name: "U", Type: reflect.TypeOf(uncommonType{})},
{Name: "M", Type: reflect.ArrayOf(mcount, reflect.TypeOf(method{}))},
}))
st := (*sliceType)(unsafe.Pointer(tt.Elem().Field(0).UnsafeAddr()))
st.elem = totype(styp.Elem())
case reflect.Array:
tt = reflect.New(reflect.StructOf([]reflect.StructField{
{Name: "S", Type: reflect.TypeOf(arrayType{})},
{Name: "U", Type: reflect.TypeOf(uncommonType{})},
{Name: "M", Type: reflect.ArrayOf(mcount, reflect.TypeOf(method{}))},
}))
st := (*arrayType)(unsafe.Pointer(tt.Elem().Field(0).UnsafeAddr()))
ost := (*arrayType)(unsafe.Pointer(ort))
st.elem = ost.elem
st.slice = ost.slice
st.len = ost.len
case reflect.Chan:
tt = reflect.New(reflect.StructOf([]reflect.StructField{
{Name: "S", Type: reflect.TypeOf(chanType{})},
{Name: "U", Type: reflect.TypeOf(uncommonType{})},
{Name: "M", Type: reflect.ArrayOf(mcount, reflect.TypeOf(method{}))},
}))
st := (*chanType)(unsafe.Pointer(tt.Elem().Field(0).UnsafeAddr()))
ost := (*chanType)(unsafe.Pointer(ort))
st.elem = ost.elem
st.dir = ost.dir
case reflect.Func:
numIn := styp.NumIn()
numOut := styp.NumOut()
narg := numIn + numOut
tt = reflect.New(reflect.StructOf([]reflect.StructField{
{Name: "S", Type: reflect.TypeOf(funcType{})},
{Name: "U", Type: reflect.TypeOf(uncommonType{})},
{Name: "N", Type: reflect.ArrayOf(narg, reflect.TypeOf((*rtype)(nil)))},
{Name: "M", Type: reflect.ArrayOf(mcount, reflect.TypeOf(method{}))},
}))
st := (*funcType)(unsafe.Pointer(tt.Elem().Field(0).UnsafeAddr()))
ost := (*funcType)(unsafe.Pointer(ort))
st.inCount = ost.inCount
st.outCount = ost.outCount
if narg > 0 {
args := make([]*rtype, narg, narg)
fnoff = uint32(unsafe.Sizeof((*rtype)(nil))) * uint32(narg)
var i int
for i = 0; i < numIn; i++ {
args[i] = totype(styp.In(i))
}
for j := 0; j < numOut; j++ {
args[i+j] = totype(styp.Out(j))
}
copy(tt.Elem().Field(2).Slice(0, narg).Interface().([]*rtype), args)
}
case reflect.Map:
tt = reflect.New(reflect.StructOf([]reflect.StructField{
{Name: "S", Type: reflect.TypeOf(mapType{})},
{Name: "U", Type: reflect.TypeOf(uncommonType{})},
{Name: "M", Type: reflect.ArrayOf(mcount, reflect.TypeOf(method{}))},
}))
st := (*mapType)(unsafe.Pointer(tt.Elem().Field(0).UnsafeAddr()))
ost := (*mapType)(unsafe.Pointer(ort))
st.key = ost.key
st.elem = ost.elem
st.bucket = ost.bucket
st.hasher = ost.hasher
st.keysize = ost.keysize
st.valuesize = ost.valuesize
st.bucketsize = ost.bucketsize
st.flags = ost.flags
default:
tt = reflect.New(reflect.StructOf([]reflect.StructField{
{Name: "S", Type: reflect.TypeOf(rtype{})},
{Name: "U", Type: reflect.TypeOf(uncommonType{})},
{Name: "M", Type: reflect.ArrayOf(mcount, reflect.TypeOf(method{}))},
}))
}
rt = (*rtype)(unsafe.Pointer(tt.Elem().Field(0).UnsafeAddr()))
rt.size = ort.size
rt.tflag = ort.tflag | tflagUncommon
rt.kind = ort.kind
rt.align = ort.align
rt.fieldAlign = ort.fieldAlign
rt.gcdata = ort.gcdata
rt.ptrdata = ort.ptrdata
rt.equal = ort.equal
rt.str = resolveReflectName(ort.nameOff(ort.str))
ut := (*uncommonType)(unsafe.Pointer(tt.Elem().Field(1).UnsafeAddr()))
ut.mcount = uint16(mcount)
ut.xcount = uint16(xcount)
ut.moff = uint32(unsafe.Sizeof(uncommonType{}))
if skind == reflect.Interface {
return rt, nil
} else if skind == reflect.Func {
ut.moff += fnoff
return rt, tt.Elem().Field(3).Slice(0, mcount).Interface().([]method)
}
return rt, tt.Elem().Field(2).Slice(0, mcount).Interface().([]method)
}
func NamedTypeOf(pkgpath string, name string, from reflect.Type) reflect.Type {
rt, _ := newType(pkgpath, name, from, 0, 0)
setTypeName(rt, pkgpath, name)
return toType(rt)
}
//go:linkname typesByString reflect.typesByString
func typesByString(s string) []*rtype
//go:linkname typelinks reflect.typelinks
func typelinks() (sections []unsafe.Pointer, offset [][]int32)
//go:linkname rtypeOff reflect.rtypeOff
func rtypeOff(section unsafe.Pointer, off int32) *rtype
func TypeLinks() []reflect.Type {
var r []reflect.Type
sections, offset := typelinks()
for i, offs := range offset {
rodata := sections[i]
for _, off := range offs {
typ := (*rtype)(resolveTypeOff(unsafe.Pointer(rodata), off))
r = append(r, toType(typ))
}
}
return r
}
func TypesByString(s string) []reflect.Type {
sections, offset := typelinks()
var ret []reflect.Type
for offsI, offs := range offset {
section := sections[offsI]
// We are looking for the first index i where the string becomes >= s.
// This is a copy of sort.Search, with f(h) replaced by (*typ[h].String() >= s).
i, j := 0, len(offs)
for i < j {
h := i + (j-i)/2 // avoid overflow when computing h
// i ≤ h < j
typ := toType(rtypeOff(section, offs[h]))
if !(typ.String() >= s) {
i = h + 1 // preserves f(i-1) == false
} else {
j = h // preserves f(j) == true
}
}
// i == j, f(i-1) == false, and f(j) (= f(i)) == true => answer is i.
// Having found the first, linear scan forward to find the last.
// We could do a second binary search, but the caller is going
// to do a linear scan anyway.
for j := i; j < len(offs); j++ {
typ := toType(rtypeOff(section, offs[j]))
if typ.String() != s {
break
}
ret = append(ret, typ)
}
}
return ret
}
func DumpType(w io.Writer, typ reflect.Type) {
rt := totype(typ)
fmt.Fprintf(w, "%#v\n", rt)
for _, m := range rt.methods() {
fmt.Fprintf(w, "%v %v (%v)\t\t%#v\n",
rt.nameOff(m.name).name(),
rt.nameOff(m.name).pkgPath(),
toType(rt.typeOff(m.mtyp)),
m)
}
}
func NumMethodX(typ reflect.Type) int {
return totype(typ).NumMethodX()
}
func MethodX(typ reflect.Type, i int) reflect.Method {
return totype(typ).MethodX(i)
}
func (t *rtype) NumMethodX() int {
return len(t.methods())
}
func (t *rtype) MethodX(i int) (m reflect.Method) {
if t.Kind() == reflect.Interface {
return toType(t).Method(i)
}
methods := t.methods()
if i < 0 || i >= len(methods) {
panic("reflect: Method index out of range")
}
p := methods[i]
pname := t.nameOff(p.name)
m.Name = pname.name()
m.Index = i
fl := flag(reflect.Func)
if t.tflag&tflagUserMethod != 0 {
fl |= flagIndir
}
mtyp := t.typeOff(p.mtyp)
if mtyp == nil {
return
}
ft := (*funcType)(unsafe.Pointer(mtyp))
in := make([]reflect.Type, 0, 1+len(ft.in()))
in = append(in, toType(t))
for _, arg := range ft.in() {
in = append(in, toType(arg))
}
out := make([]reflect.Type, 0, len(ft.out()))
for _, ret := range ft.out() {
out = append(out, toType(ret))
}
mt := reflect.FuncOf(in, out, ft.IsVariadic())
m.Type = mt
tfn := t.textOff(p.tfn)
fn := unsafe.Pointer(&tfn)
m.Func = toValue(Value{totype(mt), fn, fl})
return m
}
func (t *rtype) MethodByNameX(name string) (m reflect.Method, ok bool) {
if t.Kind() == reflect.Interface {
return toType(t).MethodByName(name)
}
if ut := t.uncommon(); ut != nil {
for i, p := range ut.methods() {
if t.nameOff(p.name).name() == name {
return t.MethodX(i), true
}
}
}
return reflect.Method{}, false
}
// Field returns the i'th field of the struct v.
// It panics if v's Kind is not Struct or i is out of range.
func FieldX(v reflect.Value, i int) reflect.Value {
mustBe("reflect.Value.Field", v, reflect.Struct)
rv := tovalue(&v)
tt := (*structType)(unsafe.Pointer(rv.typ))
if uint(i) >= uint(len(tt.fields)) {
panic("reflect: Field index out of range")
}
field := &tt.fields[i]
typ := field.typ
// Inherit permission bits from v, but clear flagEmbedRO.
fl := rv.flag&(flagStickyRO|flagIndir|flagAddr) | flag(typ.Kind())
// Using an unexported field forces flagRO.
// if !field.name.isExported() {
// if field.embedded() {
// fl |= flagEmbedRO
// } else {
// fl |= flagStickyRO
// }
// }
// Either flagIndir is set and v.ptr points at struct,
// or flagIndir is not set and v.ptr is the actual struct data.
// In the former case, we want v.ptr + offset.
// In the latter case, we must have field.offset = 0,
// so v.ptr + field.offset is still the correct address.
ptr := add(rv.ptr, field.offset(), "same as non-reflect &v.field")
return toValue(Value{typ, ptr, fl})
}