forked from alecthomas/gometalinter
/
reflect.go
1975 lines (1714 loc) · 54.5 KB
/
reflect.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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2013 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 pointer
// This file implements the generation and resolution rules for
// constraints arising from the use of reflection in the target
// program. See doc.go for explanation of the representation.
//
// For consistency, the names of all parameters match those of the
// actual functions in the "reflect" package.
//
// To avoid proliferation of equivalent labels, intrinsics should
// memoize as much as possible, like TypeOf and Zero do for their
// tagged objects.
//
// TODO(adonovan): this file is rather subtle. Explain how we derive
// the implementation of each reflect operator from its spec,
// including the subtleties of reflect.flag{Addr,RO,Indir}.
// [Hint: our implementation is as if reflect.flagIndir was always
// true, i.e. reflect.Values are pointers to tagged objects, there is
// no inline allocation optimization; and indirect tagged objects (not
// yet implemented) correspond to reflect.Values with
// reflect.flagAddr.]
// A picture would help too.
//
// TODO(adonovan): try factoring up the common parts of the majority of
// these constraints that are single input, single output.
import (
"fmt"
exact "go/constant"
"go/types"
"reflect"
"golang.org/x/tools/go/ssa"
)
func init() {
for name, fn := range map[string]intrinsic{
// reflect.Value methods.
"(reflect.Value).Addr": ext۰reflect۰Value۰Addr,
"(reflect.Value).Bool": ext۰NoEffect,
"(reflect.Value).Bytes": ext۰reflect۰Value۰Bytes,
"(reflect.Value).Call": ext۰reflect۰Value۰Call,
"(reflect.Value).CallSlice": ext۰reflect۰Value۰CallSlice,
"(reflect.Value).CanAddr": ext۰NoEffect,
"(reflect.Value).CanInterface": ext۰NoEffect,
"(reflect.Value).CanSet": ext۰NoEffect,
"(reflect.Value).Cap": ext۰NoEffect,
"(reflect.Value).Close": ext۰NoEffect,
"(reflect.Value).Complex": ext۰NoEffect,
"(reflect.Value).Convert": ext۰reflect۰Value۰Convert,
"(reflect.Value).Elem": ext۰reflect۰Value۰Elem,
"(reflect.Value).Field": ext۰reflect۰Value۰Field,
"(reflect.Value).FieldByIndex": ext۰reflect۰Value۰FieldByIndex,
"(reflect.Value).FieldByName": ext۰reflect۰Value۰FieldByName,
"(reflect.Value).FieldByNameFunc": ext۰reflect۰Value۰FieldByNameFunc,
"(reflect.Value).Float": ext۰NoEffect,
"(reflect.Value).Index": ext۰reflect۰Value۰Index,
"(reflect.Value).Int": ext۰NoEffect,
"(reflect.Value).Interface": ext۰reflect۰Value۰Interface,
"(reflect.Value).InterfaceData": ext۰NoEffect,
"(reflect.Value).IsNil": ext۰NoEffect,
"(reflect.Value).IsValid": ext۰NoEffect,
"(reflect.Value).Kind": ext۰NoEffect,
"(reflect.Value).Len": ext۰NoEffect,
"(reflect.Value).MapIndex": ext۰reflect۰Value۰MapIndex,
"(reflect.Value).MapKeys": ext۰reflect۰Value۰MapKeys,
"(reflect.Value).Method": ext۰reflect۰Value۰Method,
"(reflect.Value).MethodByName": ext۰reflect۰Value۰MethodByName,
"(reflect.Value).NumField": ext۰NoEffect,
"(reflect.Value).NumMethod": ext۰NoEffect,
"(reflect.Value).OverflowComplex": ext۰NoEffect,
"(reflect.Value).OverflowFloat": ext۰NoEffect,
"(reflect.Value).OverflowInt": ext۰NoEffect,
"(reflect.Value).OverflowUint": ext۰NoEffect,
"(reflect.Value).Pointer": ext۰NoEffect,
"(reflect.Value).Recv": ext۰reflect۰Value۰Recv,
"(reflect.Value).Send": ext۰reflect۰Value۰Send,
"(reflect.Value).Set": ext۰reflect۰Value۰Set,
"(reflect.Value).SetBool": ext۰NoEffect,
"(reflect.Value).SetBytes": ext۰reflect۰Value۰SetBytes,
"(reflect.Value).SetComplex": ext۰NoEffect,
"(reflect.Value).SetFloat": ext۰NoEffect,
"(reflect.Value).SetInt": ext۰NoEffect,
"(reflect.Value).SetLen": ext۰NoEffect,
"(reflect.Value).SetMapIndex": ext۰reflect۰Value۰SetMapIndex,
"(reflect.Value).SetPointer": ext۰reflect۰Value۰SetPointer,
"(reflect.Value).SetString": ext۰NoEffect,
"(reflect.Value).SetUint": ext۰NoEffect,
"(reflect.Value).Slice": ext۰reflect۰Value۰Slice,
"(reflect.Value).String": ext۰NoEffect,
"(reflect.Value).TryRecv": ext۰reflect۰Value۰Recv,
"(reflect.Value).TrySend": ext۰reflect۰Value۰Send,
"(reflect.Value).Type": ext۰NoEffect,
"(reflect.Value).Uint": ext۰NoEffect,
"(reflect.Value).UnsafeAddr": ext۰NoEffect,
// Standalone reflect.* functions.
"reflect.Append": ext۰reflect۰Append,
"reflect.AppendSlice": ext۰reflect۰AppendSlice,
"reflect.Copy": ext۰reflect۰Copy,
"reflect.ChanOf": ext۰reflect۰ChanOf,
"reflect.DeepEqual": ext۰NoEffect,
"reflect.Indirect": ext۰reflect۰Indirect,
"reflect.MakeChan": ext۰reflect۰MakeChan,
"reflect.MakeFunc": ext۰reflect۰MakeFunc,
"reflect.MakeMap": ext۰reflect۰MakeMap,
"reflect.MakeSlice": ext۰reflect۰MakeSlice,
"reflect.MapOf": ext۰reflect۰MapOf,
"reflect.New": ext۰reflect۰New,
"reflect.NewAt": ext۰reflect۰NewAt,
"reflect.PtrTo": ext۰reflect۰PtrTo,
"reflect.Select": ext۰reflect۰Select,
"reflect.SliceOf": ext۰reflect۰SliceOf,
"reflect.TypeOf": ext۰reflect۰TypeOf,
"reflect.ValueOf": ext۰reflect۰ValueOf,
"reflect.Zero": ext۰reflect۰Zero,
"reflect.init": ext۰NoEffect,
// *reflect.rtype methods
"(*reflect.rtype).Align": ext۰NoEffect,
"(*reflect.rtype).AssignableTo": ext۰NoEffect,
"(*reflect.rtype).Bits": ext۰NoEffect,
"(*reflect.rtype).ChanDir": ext۰NoEffect,
"(*reflect.rtype).ConvertibleTo": ext۰NoEffect,
"(*reflect.rtype).Elem": ext۰reflect۰rtype۰Elem,
"(*reflect.rtype).Field": ext۰reflect۰rtype۰Field,
"(*reflect.rtype).FieldAlign": ext۰NoEffect,
"(*reflect.rtype).FieldByIndex": ext۰reflect۰rtype۰FieldByIndex,
"(*reflect.rtype).FieldByName": ext۰reflect۰rtype۰FieldByName,
"(*reflect.rtype).FieldByNameFunc": ext۰reflect۰rtype۰FieldByNameFunc,
"(*reflect.rtype).Implements": ext۰NoEffect,
"(*reflect.rtype).In": ext۰reflect۰rtype۰In,
"(*reflect.rtype).IsVariadic": ext۰NoEffect,
"(*reflect.rtype).Key": ext۰reflect۰rtype۰Key,
"(*reflect.rtype).Kind": ext۰NoEffect,
"(*reflect.rtype).Len": ext۰NoEffect,
"(*reflect.rtype).Method": ext۰reflect۰rtype۰Method,
"(*reflect.rtype).MethodByName": ext۰reflect۰rtype۰MethodByName,
"(*reflect.rtype).Name": ext۰NoEffect,
"(*reflect.rtype).NumField": ext۰NoEffect,
"(*reflect.rtype).NumIn": ext۰NoEffect,
"(*reflect.rtype).NumMethod": ext۰NoEffect,
"(*reflect.rtype).NumOut": ext۰NoEffect,
"(*reflect.rtype).Out": ext۰reflect۰rtype۰Out,
"(*reflect.rtype).PkgPath": ext۰NoEffect,
"(*reflect.rtype).Size": ext۰NoEffect,
"(*reflect.rtype).String": ext۰NoEffect,
} {
intrinsicsByName[name] = fn
}
}
// -------------------- (reflect.Value) --------------------
func ext۰reflect۰Value۰Addr(a *analysis, cgn *cgnode) {} // TODO(adonovan)
// ---------- func (Value).Bytes() Value ----------
// result = v.Bytes()
type rVBytesConstraint struct {
v nodeid // (ptr)
result nodeid // (indirect)
}
func (c *rVBytesConstraint) ptr() nodeid { return c.v }
func (c *rVBytesConstraint) presolve(h *hvn) {
h.markIndirect(onodeid(c.result), "rVBytes.result")
}
func (c *rVBytesConstraint) renumber(mapping []nodeid) {
c.v = mapping[c.v]
c.result = mapping[c.result]
}
func (c *rVBytesConstraint) String() string {
return fmt.Sprintf("n%d = reflect n%d.Bytes()", c.result, c.v)
}
func (c *rVBytesConstraint) solve(a *analysis, delta *nodeset) {
changed := false
for _, x := range delta.AppendTo(a.deltaSpace) {
vObj := nodeid(x)
tDyn, slice, indirect := a.taggedValue(vObj)
if indirect {
// TODO(adonovan): we'll need to implement this
// when we start creating indirect tagged objects.
panic("indirect tagged object")
}
tSlice, ok := tDyn.Underlying().(*types.Slice)
if ok && types.Identical(tSlice.Elem(), types.Typ[types.Uint8]) {
if a.onlineCopy(c.result, slice) {
changed = true
}
}
}
if changed {
a.addWork(c.result)
}
}
func ext۰reflect۰Value۰Bytes(a *analysis, cgn *cgnode) {
a.addConstraint(&rVBytesConstraint{
v: a.funcParams(cgn.obj),
result: a.funcResults(cgn.obj),
})
}
// ---------- func (Value).Call(in []Value) []Value ----------
// result = v.Call(in)
type rVCallConstraint struct {
cgn *cgnode
targets nodeid // (indirect)
v nodeid // (ptr)
arg nodeid // = in[*]
result nodeid // (indirect)
dotdotdot bool // interpret last arg as a "..." slice
}
func (c *rVCallConstraint) ptr() nodeid { return c.v }
func (c *rVCallConstraint) presolve(h *hvn) {
h.markIndirect(onodeid(c.targets), "rVCall.targets")
h.markIndirect(onodeid(c.result), "rVCall.result")
}
func (c *rVCallConstraint) renumber(mapping []nodeid) {
c.targets = mapping[c.targets]
c.v = mapping[c.v]
c.arg = mapping[c.arg]
c.result = mapping[c.result]
}
func (c *rVCallConstraint) String() string {
return fmt.Sprintf("n%d = reflect n%d.Call(n%d)", c.result, c.v, c.arg)
}
func (c *rVCallConstraint) solve(a *analysis, delta *nodeset) {
if c.targets == 0 {
panic("no targets")
}
changed := false
for _, x := range delta.AppendTo(a.deltaSpace) {
vObj := nodeid(x)
tDyn, fn, indirect := a.taggedValue(vObj)
if indirect {
// TODO(adonovan): we'll need to implement this
// when we start creating indirect tagged objects.
panic("indirect tagged object")
}
tSig, ok := tDyn.Underlying().(*types.Signature)
if !ok {
continue // not a function
}
if tSig.Recv() != nil {
panic(tSig) // TODO(adonovan): rethink when we implement Method()
}
// Add dynamic call target.
if a.onlineCopy(c.targets, fn) {
a.addWork(c.targets)
// TODO(adonovan): is 'else continue' a sound optimisation here?
}
// Allocate a P/R block.
tParams := tSig.Params()
tResults := tSig.Results()
params := a.addNodes(tParams, "rVCall.params")
results := a.addNodes(tResults, "rVCall.results")
// Make a dynamic call to 'fn'.
a.store(fn, params, 1, a.sizeof(tParams))
a.load(results, fn, 1+a.sizeof(tParams), a.sizeof(tResults))
// Populate P by type-asserting each actual arg (all merged in c.arg).
for i, n := 0, tParams.Len(); i < n; i++ {
T := tParams.At(i).Type()
a.typeAssert(T, params, c.arg, false)
params += nodeid(a.sizeof(T))
}
// Use R by tagging and copying each actual result to c.result.
for i, n := 0, tResults.Len(); i < n; i++ {
T := tResults.At(i).Type()
// Convert from an arbitrary type to a reflect.Value
// (like MakeInterface followed by reflect.ValueOf).
if isInterface(T) {
// (don't tag)
if a.onlineCopy(c.result, results) {
changed = true
}
} else {
obj := a.makeTagged(T, c.cgn, nil)
a.onlineCopyN(obj+1, results, a.sizeof(T))
if a.addLabel(c.result, obj) { // (true)
changed = true
}
}
results += nodeid(a.sizeof(T))
}
}
if changed {
a.addWork(c.result)
}
}
// Common code for direct (inlined) and indirect calls to (reflect.Value).Call.
func reflectCallImpl(a *analysis, cgn *cgnode, site *callsite, recv, arg nodeid, dotdotdot bool) nodeid {
// Allocate []reflect.Value array for the result.
ret := a.nextNode()
a.addNodes(types.NewArray(a.reflectValueObj.Type(), 1), "rVCall.ret")
a.endObject(ret, cgn, nil)
// pts(targets) will be the set of possible call targets.
site.targets = a.addOneNode(tInvalid, "rvCall.targets", nil)
// All arguments are merged since they arrive in a slice.
argelts := a.addOneNode(a.reflectValueObj.Type(), "rVCall.args", nil)
a.load(argelts, arg, 1, 1) // slice elements
a.addConstraint(&rVCallConstraint{
cgn: cgn,
targets: site.targets,
v: recv,
arg: argelts,
result: ret + 1, // results go into elements of ret
dotdotdot: dotdotdot,
})
return ret
}
func reflectCall(a *analysis, cgn *cgnode, dotdotdot bool) {
// This is the shared contour implementation of (reflect.Value).Call
// and CallSlice, as used by indirect calls (rare).
// Direct calls are inlined in gen.go, eliding the
// intermediate cgnode for Call.
site := new(callsite)
cgn.sites = append(cgn.sites, site)
recv := a.funcParams(cgn.obj)
arg := recv + 1
ret := reflectCallImpl(a, cgn, site, recv, arg, dotdotdot)
a.addressOf(cgn.fn.Signature.Results().At(0).Type(), a.funcResults(cgn.obj), ret)
}
func ext۰reflect۰Value۰Call(a *analysis, cgn *cgnode) {
reflectCall(a, cgn, false)
}
func ext۰reflect۰Value۰CallSlice(a *analysis, cgn *cgnode) {
// TODO(adonovan): implement. Also, inline direct calls in gen.go too.
if false {
reflectCall(a, cgn, true)
}
}
func ext۰reflect۰Value۰Convert(a *analysis, cgn *cgnode) {} // TODO(adonovan)
// ---------- func (Value).Elem() Value ----------
// result = v.Elem()
type rVElemConstraint struct {
cgn *cgnode
v nodeid // (ptr)
result nodeid // (indirect)
}
func (c *rVElemConstraint) ptr() nodeid { return c.v }
func (c *rVElemConstraint) presolve(h *hvn) {
h.markIndirect(onodeid(c.result), "rVElem.result")
}
func (c *rVElemConstraint) renumber(mapping []nodeid) {
c.v = mapping[c.v]
c.result = mapping[c.result]
}
func (c *rVElemConstraint) String() string {
return fmt.Sprintf("n%d = reflect n%d.Elem()", c.result, c.v)
}
func (c *rVElemConstraint) solve(a *analysis, delta *nodeset) {
changed := false
for _, x := range delta.AppendTo(a.deltaSpace) {
vObj := nodeid(x)
tDyn, payload, indirect := a.taggedValue(vObj)
if indirect {
// TODO(adonovan): we'll need to implement this
// when we start creating indirect tagged objects.
panic("indirect tagged object")
}
switch t := tDyn.Underlying().(type) {
case *types.Interface:
if a.onlineCopy(c.result, payload) {
changed = true
}
case *types.Pointer:
obj := a.makeTagged(t.Elem(), c.cgn, nil)
a.load(obj+1, payload, 0, a.sizeof(t.Elem()))
if a.addLabel(c.result, obj) {
changed = true
}
}
}
if changed {
a.addWork(c.result)
}
}
func ext۰reflect۰Value۰Elem(a *analysis, cgn *cgnode) {
a.addConstraint(&rVElemConstraint{
cgn: cgn,
v: a.funcParams(cgn.obj),
result: a.funcResults(cgn.obj),
})
}
func ext۰reflect۰Value۰Field(a *analysis, cgn *cgnode) {} // TODO(adonovan)
func ext۰reflect۰Value۰FieldByIndex(a *analysis, cgn *cgnode) {} // TODO(adonovan)
func ext۰reflect۰Value۰FieldByName(a *analysis, cgn *cgnode) {} // TODO(adonovan)
func ext۰reflect۰Value۰FieldByNameFunc(a *analysis, cgn *cgnode) {} // TODO(adonovan)
// ---------- func (Value).Index() Value ----------
// result = v.Index()
type rVIndexConstraint struct {
cgn *cgnode
v nodeid // (ptr)
result nodeid // (indirect)
}
func (c *rVIndexConstraint) ptr() nodeid { return c.v }
func (c *rVIndexConstraint) presolve(h *hvn) {
h.markIndirect(onodeid(c.result), "rVIndex.result")
}
func (c *rVIndexConstraint) renumber(mapping []nodeid) {
c.v = mapping[c.v]
c.result = mapping[c.result]
}
func (c *rVIndexConstraint) String() string {
return fmt.Sprintf("n%d = reflect n%d.Index()", c.result, c.v)
}
func (c *rVIndexConstraint) solve(a *analysis, delta *nodeset) {
changed := false
for _, x := range delta.AppendTo(a.deltaSpace) {
vObj := nodeid(x)
tDyn, payload, indirect := a.taggedValue(vObj)
if indirect {
// TODO(adonovan): we'll need to implement this
// when we start creating indirect tagged objects.
panic("indirect tagged object")
}
var res nodeid
switch t := tDyn.Underlying().(type) {
case *types.Array:
res = a.makeTagged(t.Elem(), c.cgn, nil)
a.onlineCopyN(res+1, payload+1, a.sizeof(t.Elem()))
case *types.Slice:
res = a.makeTagged(t.Elem(), c.cgn, nil)
a.load(res+1, payload, 1, a.sizeof(t.Elem()))
case *types.Basic:
if t.Kind() == types.String {
res = a.makeTagged(types.Typ[types.Rune], c.cgn, nil)
}
}
if res != 0 && a.addLabel(c.result, res) {
changed = true
}
}
if changed {
a.addWork(c.result)
}
}
func ext۰reflect۰Value۰Index(a *analysis, cgn *cgnode) {
a.addConstraint(&rVIndexConstraint{
cgn: cgn,
v: a.funcParams(cgn.obj),
result: a.funcResults(cgn.obj),
})
}
// ---------- func (Value).Interface() Value ----------
// result = v.Interface()
type rVInterfaceConstraint struct {
v nodeid // (ptr)
result nodeid // (indirect)
}
func (c *rVInterfaceConstraint) ptr() nodeid { return c.v }
func (c *rVInterfaceConstraint) presolve(h *hvn) {
h.markIndirect(onodeid(c.result), "rVInterface.result")
}
func (c *rVInterfaceConstraint) renumber(mapping []nodeid) {
c.v = mapping[c.v]
c.result = mapping[c.result]
}
func (c *rVInterfaceConstraint) String() string {
return fmt.Sprintf("n%d = reflect n%d.Interface()", c.result, c.v)
}
func (c *rVInterfaceConstraint) solve(a *analysis, delta *nodeset) {
changed := false
for _, x := range delta.AppendTo(a.deltaSpace) {
vObj := nodeid(x)
tDyn, payload, indirect := a.taggedValue(vObj)
if indirect {
// TODO(adonovan): we'll need to implement this
// when we start creating indirect tagged objects.
panic("indirect tagged object")
}
if isInterface(tDyn) {
if a.onlineCopy(c.result, payload) {
a.addWork(c.result)
}
} else {
if a.addLabel(c.result, vObj) {
changed = true
}
}
}
if changed {
a.addWork(c.result)
}
}
func ext۰reflect۰Value۰Interface(a *analysis, cgn *cgnode) {
a.addConstraint(&rVInterfaceConstraint{
v: a.funcParams(cgn.obj),
result: a.funcResults(cgn.obj),
})
}
// ---------- func (Value).MapIndex(Value) Value ----------
// result = v.MapIndex(_)
type rVMapIndexConstraint struct {
cgn *cgnode
v nodeid // (ptr)
result nodeid // (indirect)
}
func (c *rVMapIndexConstraint) ptr() nodeid { return c.v }
func (c *rVMapIndexConstraint) presolve(h *hvn) {
h.markIndirect(onodeid(c.result), "rVMapIndex.result")
}
func (c *rVMapIndexConstraint) renumber(mapping []nodeid) {
c.v = mapping[c.v]
c.result = mapping[c.result]
}
func (c *rVMapIndexConstraint) String() string {
return fmt.Sprintf("n%d = reflect n%d.MapIndex(_)", c.result, c.v)
}
func (c *rVMapIndexConstraint) solve(a *analysis, delta *nodeset) {
changed := false
for _, x := range delta.AppendTo(a.deltaSpace) {
vObj := nodeid(x)
tDyn, m, indirect := a.taggedValue(vObj)
tMap, _ := tDyn.Underlying().(*types.Map)
if tMap == nil {
continue // not a map
}
if indirect {
// TODO(adonovan): we'll need to implement this
// when we start creating indirect tagged objects.
panic("indirect tagged object")
}
obj := a.makeTagged(tMap.Elem(), c.cgn, nil)
a.load(obj+1, m, a.sizeof(tMap.Key()), a.sizeof(tMap.Elem()))
if a.addLabel(c.result, obj) {
changed = true
}
}
if changed {
a.addWork(c.result)
}
}
func ext۰reflect۰Value۰MapIndex(a *analysis, cgn *cgnode) {
a.addConstraint(&rVMapIndexConstraint{
cgn: cgn,
v: a.funcParams(cgn.obj),
result: a.funcResults(cgn.obj),
})
}
// ---------- func (Value).MapKeys() []Value ----------
// result = v.MapKeys()
type rVMapKeysConstraint struct {
cgn *cgnode
v nodeid // (ptr)
result nodeid // (indirect)
}
func (c *rVMapKeysConstraint) ptr() nodeid { return c.v }
func (c *rVMapKeysConstraint) presolve(h *hvn) {
h.markIndirect(onodeid(c.result), "rVMapKeys.result")
}
func (c *rVMapKeysConstraint) renumber(mapping []nodeid) {
c.v = mapping[c.v]
c.result = mapping[c.result]
}
func (c *rVMapKeysConstraint) String() string {
return fmt.Sprintf("n%d = reflect n%d.MapKeys()", c.result, c.v)
}
func (c *rVMapKeysConstraint) solve(a *analysis, delta *nodeset) {
changed := false
for _, x := range delta.AppendTo(a.deltaSpace) {
vObj := nodeid(x)
tDyn, m, indirect := a.taggedValue(vObj)
tMap, _ := tDyn.Underlying().(*types.Map)
if tMap == nil {
continue // not a map
}
if indirect {
// TODO(adonovan): we'll need to implement this
// when we start creating indirect tagged objects.
panic("indirect tagged object")
}
kObj := a.makeTagged(tMap.Key(), c.cgn, nil)
a.load(kObj+1, m, 0, a.sizeof(tMap.Key()))
if a.addLabel(c.result, kObj) {
changed = true
}
}
if changed {
a.addWork(c.result)
}
}
func ext۰reflect۰Value۰MapKeys(a *analysis, cgn *cgnode) {
// Allocate an array for the result.
obj := a.nextNode()
T := types.NewSlice(a.reflectValueObj.Type())
a.addNodes(sliceToArray(T), "reflect.MapKeys result")
a.endObject(obj, cgn, nil)
a.addressOf(T, a.funcResults(cgn.obj), obj)
a.addConstraint(&rVMapKeysConstraint{
cgn: cgn,
v: a.funcParams(cgn.obj),
result: obj + 1, // result is stored in array elems
})
}
func ext۰reflect۰Value۰Method(a *analysis, cgn *cgnode) {} // TODO(adonovan)
func ext۰reflect۰Value۰MethodByName(a *analysis, cgn *cgnode) {} // TODO(adonovan)
// ---------- func (Value).Recv(Value) Value ----------
// result, _ = v.Recv()
type rVRecvConstraint struct {
cgn *cgnode
v nodeid // (ptr)
result nodeid // (indirect)
}
func (c *rVRecvConstraint) ptr() nodeid { return c.v }
func (c *rVRecvConstraint) presolve(h *hvn) {
h.markIndirect(onodeid(c.result), "rVRecv.result")
}
func (c *rVRecvConstraint) renumber(mapping []nodeid) {
c.v = mapping[c.v]
c.result = mapping[c.result]
}
func (c *rVRecvConstraint) String() string {
return fmt.Sprintf("n%d = reflect n%d.Recv()", c.result, c.v)
}
func (c *rVRecvConstraint) solve(a *analysis, delta *nodeset) {
changed := false
for _, x := range delta.AppendTo(a.deltaSpace) {
vObj := nodeid(x)
tDyn, ch, indirect := a.taggedValue(vObj)
tChan, _ := tDyn.Underlying().(*types.Chan)
if tChan == nil {
continue // not a channel
}
if indirect {
// TODO(adonovan): we'll need to implement this
// when we start creating indirect tagged objects.
panic("indirect tagged object")
}
tElem := tChan.Elem()
elemObj := a.makeTagged(tElem, c.cgn, nil)
a.load(elemObj+1, ch, 0, a.sizeof(tElem))
if a.addLabel(c.result, elemObj) {
changed = true
}
}
if changed {
a.addWork(c.result)
}
}
func ext۰reflect۰Value۰Recv(a *analysis, cgn *cgnode) {
a.addConstraint(&rVRecvConstraint{
cgn: cgn,
v: a.funcParams(cgn.obj),
result: a.funcResults(cgn.obj),
})
}
// ---------- func (Value).Send(Value) ----------
// v.Send(x)
type rVSendConstraint struct {
cgn *cgnode
v nodeid // (ptr)
x nodeid
}
func (c *rVSendConstraint) ptr() nodeid { return c.v }
func (c *rVSendConstraint) presolve(*hvn) {}
func (c *rVSendConstraint) renumber(mapping []nodeid) {
c.v = mapping[c.v]
c.x = mapping[c.x]
}
func (c *rVSendConstraint) String() string {
return fmt.Sprintf("reflect n%d.Send(n%d)", c.v, c.x)
}
func (c *rVSendConstraint) solve(a *analysis, delta *nodeset) {
for _, x := range delta.AppendTo(a.deltaSpace) {
vObj := nodeid(x)
tDyn, ch, indirect := a.taggedValue(vObj)
tChan, _ := tDyn.Underlying().(*types.Chan)
if tChan == nil {
continue // not a channel
}
if indirect {
// TODO(adonovan): we'll need to implement this
// when we start creating indirect tagged objects.
panic("indirect tagged object")
}
// Extract x's payload to xtmp, then store to channel.
tElem := tChan.Elem()
xtmp := a.addNodes(tElem, "Send.xtmp")
a.typeAssert(tElem, xtmp, c.x, false)
a.store(ch, xtmp, 0, a.sizeof(tElem))
}
}
func ext۰reflect۰Value۰Send(a *analysis, cgn *cgnode) {
params := a.funcParams(cgn.obj)
a.addConstraint(&rVSendConstraint{
cgn: cgn,
v: params,
x: params + 1,
})
}
func ext۰reflect۰Value۰Set(a *analysis, cgn *cgnode) {} // TODO(adonovan)
// ---------- func (Value).SetBytes(x []byte) ----------
// v.SetBytes(x)
type rVSetBytesConstraint struct {
cgn *cgnode
v nodeid // (ptr)
x nodeid
}
func (c *rVSetBytesConstraint) ptr() nodeid { return c.v }
func (c *rVSetBytesConstraint) presolve(*hvn) {}
func (c *rVSetBytesConstraint) renumber(mapping []nodeid) {
c.v = mapping[c.v]
c.x = mapping[c.x]
}
func (c *rVSetBytesConstraint) String() string {
return fmt.Sprintf("reflect n%d.SetBytes(n%d)", c.v, c.x)
}
func (c *rVSetBytesConstraint) solve(a *analysis, delta *nodeset) {
for _, x := range delta.AppendTo(a.deltaSpace) {
vObj := nodeid(x)
tDyn, slice, indirect := a.taggedValue(vObj)
if indirect {
// TODO(adonovan): we'll need to implement this
// when we start creating indirect tagged objects.
panic("indirect tagged object")
}
tSlice, ok := tDyn.Underlying().(*types.Slice)
if ok && types.Identical(tSlice.Elem(), types.Typ[types.Uint8]) {
if a.onlineCopy(slice, c.x) {
a.addWork(slice)
}
}
}
}
func ext۰reflect۰Value۰SetBytes(a *analysis, cgn *cgnode) {
params := a.funcParams(cgn.obj)
a.addConstraint(&rVSetBytesConstraint{
cgn: cgn,
v: params,
x: params + 1,
})
}
// ---------- func (Value).SetMapIndex(k Value, v Value) ----------
// v.SetMapIndex(key, val)
type rVSetMapIndexConstraint struct {
cgn *cgnode
v nodeid // (ptr)
key nodeid
val nodeid
}
func (c *rVSetMapIndexConstraint) ptr() nodeid { return c.v }
func (c *rVSetMapIndexConstraint) presolve(*hvn) {}
func (c *rVSetMapIndexConstraint) renumber(mapping []nodeid) {
c.v = mapping[c.v]
c.key = mapping[c.key]
c.val = mapping[c.val]
}
func (c *rVSetMapIndexConstraint) String() string {
return fmt.Sprintf("reflect n%d.SetMapIndex(n%d, n%d)", c.v, c.key, c.val)
}
func (c *rVSetMapIndexConstraint) solve(a *analysis, delta *nodeset) {
for _, x := range delta.AppendTo(a.deltaSpace) {
vObj := nodeid(x)
tDyn, m, indirect := a.taggedValue(vObj)
tMap, _ := tDyn.Underlying().(*types.Map)
if tMap == nil {
continue // not a map
}
if indirect {
// TODO(adonovan): we'll need to implement this
// when we start creating indirect tagged objects.
panic("indirect tagged object")
}
keysize := a.sizeof(tMap.Key())
// Extract key's payload to keytmp, then store to map key.
keytmp := a.addNodes(tMap.Key(), "SetMapIndex.keytmp")
a.typeAssert(tMap.Key(), keytmp, c.key, false)
a.store(m, keytmp, 0, keysize)
// Extract val's payload to vtmp, then store to map value.
valtmp := a.addNodes(tMap.Elem(), "SetMapIndex.valtmp")
a.typeAssert(tMap.Elem(), valtmp, c.val, false)
a.store(m, valtmp, keysize, a.sizeof(tMap.Elem()))
}
}
func ext۰reflect۰Value۰SetMapIndex(a *analysis, cgn *cgnode) {
params := a.funcParams(cgn.obj)
a.addConstraint(&rVSetMapIndexConstraint{
cgn: cgn,
v: params,
key: params + 1,
val: params + 2,
})
}
func ext۰reflect۰Value۰SetPointer(a *analysis, cgn *cgnode) {} // TODO(adonovan)
// ---------- func (Value).Slice(v Value, i, j int) Value ----------
// result = v.Slice(_, _)
type rVSliceConstraint struct {
cgn *cgnode
v nodeid // (ptr)
result nodeid // (indirect)
}
func (c *rVSliceConstraint) ptr() nodeid { return c.v }
func (c *rVSliceConstraint) presolve(h *hvn) {
h.markIndirect(onodeid(c.result), "rVSlice.result")
}
func (c *rVSliceConstraint) renumber(mapping []nodeid) {
c.v = mapping[c.v]
c.result = mapping[c.result]
}
func (c *rVSliceConstraint) String() string {
return fmt.Sprintf("n%d = reflect n%d.Slice(_, _)", c.result, c.v)
}
func (c *rVSliceConstraint) solve(a *analysis, delta *nodeset) {
changed := false
for _, x := range delta.AppendTo(a.deltaSpace) {
vObj := nodeid(x)
tDyn, payload, indirect := a.taggedValue(vObj)
if indirect {
// TODO(adonovan): we'll need to implement this
// when we start creating indirect tagged objects.
panic("indirect tagged object")
}
var res nodeid
switch t := tDyn.Underlying().(type) {
case *types.Pointer:
if tArr, ok := t.Elem().Underlying().(*types.Array); ok {
// pointer to array
res = a.makeTagged(types.NewSlice(tArr.Elem()), c.cgn, nil)
if a.onlineCopy(res+1, payload) {
a.addWork(res + 1)
}
}
case *types.Array:
// TODO(adonovan): implement addressable
// arrays when we do indirect tagged objects.
case *types.Slice:
res = vObj
case *types.Basic:
if t == types.Typ[types.String] {
res = vObj
}
}
if res != 0 && a.addLabel(c.result, res) {
changed = true
}
}
if changed {
a.addWork(c.result)
}
}
func ext۰reflect۰Value۰Slice(a *analysis, cgn *cgnode) {
a.addConstraint(&rVSliceConstraint{
cgn: cgn,
v: a.funcParams(cgn.obj),
result: a.funcResults(cgn.obj),
})
}
// -------------------- Standalone reflect functions --------------------
func ext۰reflect۰Append(a *analysis, cgn *cgnode) {} // TODO(adonovan)
func ext۰reflect۰AppendSlice(a *analysis, cgn *cgnode) {} // TODO(adonovan)
func ext۰reflect۰Copy(a *analysis, cgn *cgnode) {} // TODO(adonovan)
// ---------- func ChanOf(ChanDir, Type) Type ----------
// result = ChanOf(dir, t)
type reflectChanOfConstraint struct {
cgn *cgnode
t nodeid // (ptr)
result nodeid // (indirect)
dirs []types.ChanDir
}
func (c *reflectChanOfConstraint) ptr() nodeid { return c.t }
func (c *reflectChanOfConstraint) presolve(h *hvn) {
h.markIndirect(onodeid(c.result), "reflectChanOf.result")
}
func (c *reflectChanOfConstraint) renumber(mapping []nodeid) {
c.t = mapping[c.t]
c.result = mapping[c.result]
}
func (c *reflectChanOfConstraint) String() string {
return fmt.Sprintf("n%d = reflect.ChanOf(n%d)", c.result, c.t)
}
func (c *reflectChanOfConstraint) solve(a *analysis, delta *nodeset) {
changed := false
for _, x := range delta.AppendTo(a.deltaSpace) {
tObj := nodeid(x)
T := a.rtypeTaggedValue(tObj)
if typeTooHigh(T) {
continue
}