summaryrefslogtreecommitdiff
path: root/src/cmd/gofix/reflect.go
blob: 74ddb398f3ac9e99d777052861accf4df3a53111 (plain)
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
// Copyright 2011 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.

// TODO(rsc): Once there is better support for writing
// multi-package commands, this should really be in
// its own package, and then we can drop all the "reflect"
// prefixes on the global variables and functions.

package main

import (
	"go/ast"
	"go/token"
	"strings"
)

var reflectFix = fix{
	"reflect",
	reflectFn,
	`Adapt code to new reflect API.

http://codereview.appspot.com/4281055
`,
}

func init() {
	register(reflectFix)
}

// The reflect API change dropped the concrete types *reflect.ArrayType etc.
// Any type assertions prior to method calls can be deleted:
//	x.(*reflect.ArrayType).Len() -> x.Len()
//
// Any type checks can be replaced by assignment and check of Kind:
//	x, y := z.(*reflect.ArrayType)
// ->
//	x := z
//	y := x.Kind() == reflect.Array
//
// If z is an ordinary variable name and x is not subsequently assigned to,
// references to x can be replaced by z and the assignment deleted.
// We only bother if x and z are the same name.  
// If y is not subsequently assigned to and neither is x, references to
// y can be replaced by its expression.  We only bother when there is
// just one use or when the use appears in an if clause.
//
// Not all type checks result in a single Kind check.  The rewrite of the type check for
// reflect.ArrayOrSliceType checks x.Kind() against reflect.Array and reflect.Slice.
// The rewrite for *reflect.IntType checks againt Int, Int8, Int16, Int32, Int64.
// The rewrite for *reflect.UintType adds Uintptr.
//
// A type switch turns into an assignment and a switch on Kind:
//	switch x := y.(type) {
//	case reflect.ArrayOrSliceType:
//		...
//	case *reflect.ChanType:
//		...
//	case *reflect.IntType:
//		...
//	}
// ->
//	switch x := y; x.Kind() {
//	case reflect.Array, reflect.Slice:
//		...
//	case reflect.Chan:
//		...
//	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
//		...
//	}
//
// The same simplification applies: we drop x := x if x is not assigned
// to in the switch cases.
//
// Because the type check assignment includes a type assertion in its
// syntax and the rewrite traversal is bottom up, we must do a pass to
// rewrite the type check assignments and then a separate pass to 
// rewrite the type assertions.
//
// The same process applies to the API changes for reflect.Value.
//
// For both cases, but especially Value, the code needs to be aware
// of the type of a receiver when rewriting a method call.   For example,
// x.(*reflect.ArrayValue).Elem(i) becomes x.Index(i) while 
// x.(*reflect.MapValue).Elem(v) becomes x.MapIndex(v).
// In general, reflectFn needs to know the type of the receiver expression.
// In most cases (and in all the cases in the Go source tree), the toy
// type checker in typecheck.go provides enough information for gofix
// to make the rewrite.  If gofix misses a rewrite, the code that is left over
// will not compile, so it will be noticed immediately.

func reflectFn(f *ast.File) bool {
	if !imports(f, "reflect") {
		return false
	}

	fixed := false

	// Rewrite names in method calls.
	// Needs basic type information (see above).
	typeof := typecheck(reflectTypeConfig, f)
	walk(f, func(n interface{}) {
		switch n := n.(type) {
		case *ast.SelectorExpr:
			typ := typeof[n.X]
			if m := reflectRewriteMethod[typ]; m != nil {
				if replace := m[n.Sel.Name]; replace != "" {
					n.Sel.Name = replace
					fixed = true
					return
				}
			}

			// For all reflect Values, replace SetValue with Set.
			if isReflectValue[typ] && n.Sel.Name == "SetValue" {
				n.Sel.Name = "Set"
				fixed = true
				return
			}

			// Replace reflect.MakeZero with reflect.Zero.
			if isPkgDot(n, "reflect", "MakeZero") {
				n.Sel.Name = "Zero"
				fixed = true
				return
			}
		}
	})

	// Replace PtrValue's PointTo(x) with Set(x.Addr()).
	walk(f, func(n interface{}) {
		call, ok := n.(*ast.CallExpr)
		if !ok || len(call.Args) != 1 {
			return
		}
		sel, ok := call.Fun.(*ast.SelectorExpr)
		if !ok || sel.Sel.Name != "PointTo" {
			return
		}
		typ := typeof[sel.X]
		if typ != "*reflect.PtrValue" {
			return
		}
		sel.Sel.Name = "Set"
		if !isTopName(call.Args[0], "nil") {
			call.Args[0] = &ast.SelectorExpr{
				X:   call.Args[0],
				Sel: ast.NewIdent("Addr()"),
			}
		}
		fixed = true
	})

	// Fix type switches.
	walk(f, func(n interface{}) {
		if reflectFixSwitch(n) {
			fixed = true
		}
	})

	// Fix type assertion checks (multiple assignment statements).
	// Have to work on the statement context (statement list or if statement)
	// so that we can insert an extra statement occasionally.
	// Ignoring for and switch because they don't come up in
	// typical code.
	walk(f, func(n interface{}) {
		switch n := n.(type) {
		case *[]ast.Stmt:
			// v is the replacement statement list.
			var v []ast.Stmt
			insert := func(x ast.Stmt) {
				v = append(v, x)
			}
			for i, x := range *n {
				// Tentatively append to v; if we rewrite x
				// we'll have to update the entry, so remember
				// the index.
				j := len(v)
				v = append(v, x)
				if reflectFixTypecheck(&x, insert, (*n)[i+1:]) {
					// reflectFixTypecheck may have overwritten x.
					// Update the entry we appended just before the call.
					v[j] = x
					fixed = true
				}
			}
			*n = v
		case *ast.IfStmt:
			x := &ast.ExprStmt{n.Cond}
			if reflectFixTypecheck(&n.Init, nil, []ast.Stmt{x, n.Body, n.Else}) {
				n.Cond = x.X
				fixed = true
			}
		}
	})

	// Warn about any typecheck statements that we missed.
	walk(f, reflectWarnTypecheckStmt)

	// Now that those are gone, fix remaining type assertions.
	// Delayed because the type checks have
	// type assertions as part of their syntax.
	walk(f, func(n interface{}) {
		if reflectFixAssert(n) {
			fixed = true
		}
	})

	// Now that the type assertions are gone, rewrite remaining
	// references to specific reflect types to use the general ones.
	walk(f, func(n interface{}) {
		ptr, ok := n.(*ast.Expr)
		if !ok {
			return
		}
		nn := *ptr
		typ := reflectType(nn)
		if typ == "" {
			return
		}
		if strings.HasSuffix(typ, "Type") {
			*ptr = newPkgDot(nn.Pos(), "reflect", "Type")
		} else {
			*ptr = newPkgDot(nn.Pos(), "reflect", "Value")
		}
		fixed = true
	})

	// Rewrite v.Set(nil) to v.Set(reflect.MakeZero(v.Type())).
	walk(f, func(n interface{}) {
		call, ok := n.(*ast.CallExpr)
		if !ok || len(call.Args) != 1 || !isTopName(call.Args[0], "nil") {
			return
		}
		sel, ok := call.Fun.(*ast.SelectorExpr)
		if !ok || !isReflectValue[typeof[sel.X]] || sel.Sel.Name != "Set" {
			return
		}
		call.Args[0] = &ast.CallExpr{
			Fun: newPkgDot(call.Args[0].Pos(), "reflect", "Zero"),
			Args: []ast.Expr{
				&ast.CallExpr{
					Fun: &ast.SelectorExpr{
						X:   sel.X,
						Sel: &ast.Ident{Name: "Type"},
					},
				},
			},
		}
		fixed = true
	})

	// Rewrite v != nil to v.IsValid().
	// Rewrite nil used as reflect.Value (in function argument or return) to reflect.Value{}.
	walk(f, func(n interface{}) {
		ptr, ok := n.(*ast.Expr)
		if !ok {
			return
		}
		if isTopName(*ptr, "nil") && isReflectValue[typeof[*ptr]] {
			*ptr = ast.NewIdent("reflect.Value{}")
			fixed = true
			return
		}
		nn, ok := (*ptr).(*ast.BinaryExpr)
		if !ok || (nn.Op != token.EQL && nn.Op != token.NEQ) || !isTopName(nn.Y, "nil") || !isReflectValue[typeof[nn.X]] {
			return
		}
		var call ast.Expr = &ast.CallExpr{
			Fun: &ast.SelectorExpr{
				X:   nn.X,
				Sel: &ast.Ident{Name: "IsValid"},
			},
		}
		if nn.Op == token.EQL {
			call = &ast.UnaryExpr{Op: token.NOT, X: call}
		}
		*ptr = call
		fixed = true
	})

	return fixed
}

// reflectFixSwitch rewrites *n (if n is an *ast.Stmt) corresponding
// to a type switch.
func reflectFixSwitch(n interface{}) bool {
	ptr, ok := n.(*ast.Stmt)
	if !ok {
		return false
	}
	n = *ptr

	ts, ok := n.(*ast.TypeSwitchStmt)
	if !ok {
		return false
	}

	// Are any switch cases referring to reflect types?
	// (That is, is this an old reflect type switch?)
	for _, cas := range ts.Body.List {
		for _, typ := range cas.(*ast.CaseClause).List {
			if reflectType(typ) != "" {
				goto haveReflect
			}
		}
	}
	return false

haveReflect:
	// Now we know it's an old reflect type switch.  Prepare the new version,
	// but don't replace or edit the original until we're sure of success.

	// Figure out the initializer statement, if any, and the receiver for the Kind call.
	var init ast.Stmt
	var rcvr ast.Expr

	init = ts.Init
	switch n := ts.Assign.(type) {
	default:
		warn(ts.Pos(), "unexpected form in type switch")
		return false

	case *ast.AssignStmt:
		as := n
		ta := as.Rhs[0].(*ast.TypeAssertExpr)
		x := isIdent(as.Lhs[0])
		z := isIdent(ta.X)

		if isBlank(x) || x != nil && z != nil && x.Name == z.Name && !assignsTo(x, ts.Body.List) {
			// Can drop the variable creation.
			rcvr = ta.X
		} else {
			// Need to use initialization statement.
			if init != nil {
				warn(ts.Pos(), "cannot rewrite reflect type switch with initializing statement")
				return false
			}
			init = &ast.AssignStmt{
				Lhs:    []ast.Expr{as.Lhs[0]},
				TokPos: as.TokPos,
				Tok:    token.DEFINE,
				Rhs:    []ast.Expr{ta.X},
			}
			rcvr = as.Lhs[0]
		}

	case *ast.ExprStmt:
		rcvr = n.X.(*ast.TypeAssertExpr).X
	}

	// Prepare rewritten type switch (see large comment above for form).
	sw := &ast.SwitchStmt{
		Switch: ts.Switch,
		Init:   init,
		Tag: &ast.CallExpr{
			Fun: &ast.SelectorExpr{
				X: rcvr,
				Sel: &ast.Ident{
					NamePos: rcvr.End(),
					Name:    "Kind",
					Obj:     nil,
				},
			},
			Lparen: rcvr.End(),
			Rparen: rcvr.End(),
		},
		Body: &ast.BlockStmt{
			Lbrace: ts.Body.Lbrace,
			List:   nil, // to be filled in
			Rbrace: ts.Body.Rbrace,
		},
	}

	// Translate cases.
	for _, tcas := range ts.Body.List {
		tcas := tcas.(*ast.CaseClause)
		cas := &ast.CaseClause{
			Case:  tcas.Case,
			Colon: tcas.Colon,
			Body:  tcas.Body,
		}
		for _, t := range tcas.List {
			if isTopName(t, "nil") {
				cas.List = append(cas.List, newPkgDot(t.Pos(), "reflect", "Invalid"))
				continue
			}

			typ := reflectType(t)
			if typ == "" {
				warn(t.Pos(), "cannot rewrite reflect type switch case with non-reflect type %s", gofmt(t))
				cas.List = append(cas.List, t)
				continue
			}

			for _, k := range reflectKind[typ] {
				cas.List = append(cas.List, newPkgDot(t.Pos(), "reflect", k))
			}
		}
		sw.Body.List = append(sw.Body.List, cas)
	}

	// Everything worked.  Rewrite AST.
	*ptr = sw
	return true
}

// Rewrite x, y = z.(T) into
//	x = z
//	y = x.Kind() == K
// as described in the long comment above.
//
// If insert != nil, it can be called to insert a statement after *ptr in its block.
// If insert == nil, insertion is not possible.
// At most one call to insert is allowed.
//
// Scope gives the statements for which a declaration
// in *ptr would be in scope.
//
// The result is true of the statement was rewritten.
//
func reflectFixTypecheck(ptr *ast.Stmt, insert func(ast.Stmt), scope []ast.Stmt) bool {
	st := *ptr
	as, ok := st.(*ast.AssignStmt)
	if !ok || len(as.Lhs) != 2 || len(as.Rhs) != 1 {
		return false
	}

	ta, ok := as.Rhs[0].(*ast.TypeAssertExpr)
	if !ok {
		return false
	}
	typ := reflectType(ta.Type)
	if typ == "" {
		return false
	}

	// Have x, y := z.(t).
	x := isIdent(as.Lhs[0])
	y := isIdent(as.Lhs[1])
	z := isIdent(ta.X)

	// First step is x := z, unless it's x := x and the resulting x is never reassigned.
	// rcvr is the x in x.Kind().
	var rcvr ast.Expr
	if isBlank(x) ||
		as.Tok == token.DEFINE && x != nil && z != nil && x.Name == z.Name && !assignsTo(x, scope) {
		// Can drop the statement.
		// If we need to insert a statement later, now we have a slot.
		*ptr = &ast.EmptyStmt{}
		insert = func(x ast.Stmt) { *ptr = x }
		rcvr = ta.X
	} else {
		*ptr = &ast.AssignStmt{
			Lhs:    []ast.Expr{as.Lhs[0]},
			TokPos: as.TokPos,
			Tok:    as.Tok,
			Rhs:    []ast.Expr{ta.X},
		}
		rcvr = as.Lhs[0]
	}

	// Prepare x.Kind() == T expression appropriate to t.
	// If x is not a simple identifier, warn that we might be
	// reevaluating x.
	if x == nil {
		warn(as.Pos(), "rewrite reevaluates expr with possible side effects: %s", gofmt(as.Lhs[0]))
	}
	yExpr, yNotExpr := reflectKindEq(rcvr, reflectKind[typ])

	// Second step is y := x.Kind() == T, unless it's only used once
	// or we have no way to insert that statement.
	var yStmt *ast.AssignStmt
	if as.Tok == token.DEFINE && countUses(y, scope) <= 1 || insert == nil {
		// Can drop the statement and use the expression directly.
		rewriteUses(y,
			func(token.Pos) ast.Expr { return yExpr },
			func(token.Pos) ast.Expr { return yNotExpr },
			scope)
	} else {
		yStmt = &ast.AssignStmt{
			Lhs:    []ast.Expr{as.Lhs[1]},
			TokPos: as.End(),
			Tok:    as.Tok,
			Rhs:    []ast.Expr{yExpr},
		}
		insert(yStmt)
	}
	return true
}

// reflectKindEq returns the expression z.Kind() == kinds[0] || z.Kind() == kinds[1] || ...
// and its negation.
// The qualifier "reflect." is inserted before each kinds[i] expression.
func reflectKindEq(z ast.Expr, kinds []string) (ast.Expr, ast.Expr) {
	n := len(kinds)
	if n == 1 {
		y := &ast.BinaryExpr{
			X: &ast.CallExpr{
				Fun: &ast.SelectorExpr{
					X:   z,
					Sel: ast.NewIdent("Kind"),
				},
			},
			Op: token.EQL,
			Y:  newPkgDot(token.NoPos, "reflect", kinds[0]),
		}
		ynot := &ast.BinaryExpr{
			X: &ast.CallExpr{
				Fun: &ast.SelectorExpr{
					X:   z,
					Sel: ast.NewIdent("Kind"),
				},
			},
			Op: token.NEQ,
			Y:  newPkgDot(token.NoPos, "reflect", kinds[0]),
		}
		return y, ynot
	}

	x, xnot := reflectKindEq(z, kinds[0:n-1])
	y, ynot := reflectKindEq(z, kinds[n-1:])

	or := &ast.BinaryExpr{
		X:  x,
		Op: token.LOR,
		Y:  y,
	}
	andnot := &ast.BinaryExpr{
		X:  xnot,
		Op: token.LAND,
		Y:  ynot,
	}
	return or, andnot
}

// if x represents a known old reflect type/value like *reflect.PtrType or reflect.ArrayOrSliceValue,
// reflectType returns the string form of that type.
func reflectType(x ast.Expr) string {
	ptr, ok := x.(*ast.StarExpr)
	if ok {
		x = ptr.X
	}

	sel, ok := x.(*ast.SelectorExpr)
	if !ok || !isName(sel.X, "reflect") {
		return ""
	}

	var s = "reflect."
	if ptr != nil {
		s = "*reflect."
	}
	s += sel.Sel.Name

	if reflectKind[s] != nil {
		return s
	}
	return ""
}

// reflectWarnTypecheckStmt warns about statements
// of the form x, y = z.(T) for any old reflect type T.
// The last pass should have gotten them all, and if it didn't,
// the next pass is going to turn them into x, y = z.
func reflectWarnTypecheckStmt(n interface{}) {
	as, ok := n.(*ast.AssignStmt)
	if !ok || len(as.Lhs) != 2 || len(as.Rhs) != 1 {
		return
	}
	ta, ok := as.Rhs[0].(*ast.TypeAssertExpr)
	if !ok || reflectType(ta.Type) == "" {
		return
	}
	warn(n.(ast.Node).Pos(), "unfixed reflect type check")
}

// reflectFixAssert rewrites x.(T) to x for any old reflect type T.
func reflectFixAssert(n interface{}) bool {
	ptr, ok := n.(*ast.Expr)
	if ok {
		ta, ok := (*ptr).(*ast.TypeAssertExpr)
		if ok && reflectType(ta.Type) != "" {
			*ptr = ta.X
			return true
		}
	}
	return false
}

// Tables describing the transformations.

// Description of old reflect API for partial type checking.
// We pretend the Elem method is on Type and Value instead
// of enumerating all the types it is actually on.
// Also, we pretend that ArrayType etc embeds Type for the
// purposes of describing the API.  (In fact they embed commonType,
// which implements Type.)
var reflectTypeConfig = &TypeConfig{
	Type: map[string]*Type{
		"reflect.ArrayOrSliceType":  &Type{Embed: []string{"reflect.Type"}},
		"reflect.ArrayOrSliceValue": &Type{Embed: []string{"reflect.Value"}},
		"reflect.ArrayType":         &Type{Embed: []string{"reflect.Type"}},
		"reflect.ArrayValue":        &Type{Embed: []string{"reflect.Value"}},
		"reflect.BoolType":          &Type{Embed: []string{"reflect.Type"}},
		"reflect.BoolValue":         &Type{Embed: []string{"reflect.Value"}},
		"reflect.ChanType":          &Type{Embed: []string{"reflect.Type"}},
		"reflect.ChanValue": &Type{
			Method: map[string]string{
				"Recv":    "func() (reflect.Value, bool)",
				"TryRecv": "func() (reflect.Value, bool)",
			},
			Embed: []string{"reflect.Value"},
		},
		"reflect.ComplexType":  &Type{Embed: []string{"reflect.Type"}},
		"reflect.ComplexValue": &Type{Embed: []string{"reflect.Value"}},
		"reflect.FloatType":    &Type{Embed: []string{"reflect.Type"}},
		"reflect.FloatValue":   &Type{Embed: []string{"reflect.Value"}},
		"reflect.FuncType": &Type{
			Method: map[string]string{
				"In":  "func(int) reflect.Type",
				"Out": "func(int) reflect.Type",
			},
			Embed: []string{"reflect.Type"},
		},
		"reflect.FuncValue": &Type{
			Method: map[string]string{
				"Call": "func([]reflect.Value) []reflect.Value",
			},
		},
		"reflect.IntType":        &Type{Embed: []string{"reflect.Type"}},
		"reflect.IntValue":       &Type{Embed: []string{"reflect.Value"}},
		"reflect.InterfaceType":  &Type{Embed: []string{"reflect.Type"}},
		"reflect.InterfaceValue": &Type{Embed: []string{"reflect.Value"}},
		"reflect.MapType": &Type{
			Method: map[string]string{
				"Key": "func() reflect.Type",
			},
			Embed: []string{"reflect.Type"},
		},
		"reflect.MapValue": &Type{
			Method: map[string]string{
				"Keys": "func() []reflect.Value",
			},
			Embed: []string{"reflect.Value"},
		},
		"reflect.Method": &Type{
			Field: map[string]string{
				"Type": "*reflect.FuncType",
				"Func": "*reflect.FuncValue",
			},
		},
		"reflect.PtrType":   &Type{Embed: []string{"reflect.Type"}},
		"reflect.PtrValue":  &Type{Embed: []string{"reflect.Value"}},
		"reflect.SliceType": &Type{Embed: []string{"reflect.Type"}},
		"reflect.SliceValue": &Type{
			Method: map[string]string{
				"Slice": "func(int, int) *reflect.SliceValue",
			},
			Embed: []string{"reflect.Value"},
		},
		"reflect.StringType":  &Type{Embed: []string{"reflect.Type"}},
		"reflect.StringValue": &Type{Embed: []string{"reflect.Value"}},
		"reflect.StructField": &Type{
			Field: map[string]string{
				"Type": "reflect.Type",
			},
		},
		"reflect.StructType": &Type{
			Method: map[string]string{
				"Field":           "func() reflect.StructField",
				"FieldByIndex":    "func() reflect.StructField",
				"FieldByName":     "func() reflect.StructField,bool",
				"FieldByNameFunc": "func() reflect.StructField,bool",
			},
			Embed: []string{"reflect.Type"},
		},
		"reflect.StructValue": &Type{
			Method: map[string]string{
				"Field":           "func() reflect.Value",
				"FieldByIndex":    "func() reflect.Value",
				"FieldByName":     "func() reflect.Value",
				"FieldByNameFunc": "func() reflect.Value",
			},
			Embed: []string{"reflect.Value"},
		},
		"reflect.Type": &Type{
			Method: map[string]string{
				"Elem":   "func() reflect.Type",
				"Method": "func() reflect.Method",
			},
		},
		"reflect.UintType":           &Type{Embed: []string{"reflect.Type"}},
		"reflect.UintValue":          &Type{Embed: []string{"reflect.Value"}},
		"reflect.UnsafePointerType":  &Type{Embed: []string{"reflect.Type"}},
		"reflect.UnsafePointerValue": &Type{Embed: []string{"reflect.Value"}},
		"reflect.Value": &Type{
			Method: map[string]string{
				"Addr":     "func() *reflect.PtrValue",
				"Elem":     "func() reflect.Value",
				"Method":   "func() *reflect.FuncValue",
				"SetValue": "func(reflect.Value)",
			},
		},
	},
	Func: map[string]string{
		"reflect.Append":      "*reflect.SliceValue",
		"reflect.AppendSlice": "*reflect.SliceValue",
		"reflect.Indirect":    "reflect.Value",
		"reflect.MakeSlice":   "*reflect.SliceValue",
		"reflect.MakeChan":    "*reflect.ChanValue",
		"reflect.MakeMap":     "*reflect.MapValue",
		"reflect.MakeZero":    "reflect.Value",
		"reflect.NewValue":    "reflect.Value",
		"reflect.PtrTo":       "*reflect.PtrType",
		"reflect.Typeof":      "reflect.Type",
	},
}

var reflectRewriteMethod = map[string]map[string]string{
	// The type API didn't change much.
	"*reflect.ChanType": {"Dir": "ChanDir"},
	"*reflect.FuncType": {"DotDotDot": "IsVariadic"},

	// The value API has longer names to disambiguate
	// methods with different signatures.
	"reflect.ArrayOrSliceValue": { // interface, not pointer
		"Elem": "Index",
	},
	"*reflect.ArrayValue": {
		"Elem": "Index",
	},
	"*reflect.BoolValue": {
		"Get": "Bool",
		"Set": "SetBool",
	},
	"*reflect.ChanValue": {
		"Get": "Pointer",
	},
	"*reflect.ComplexValue": {
		"Get":      "Complex",
		"Set":      "SetComplex",
		"Overflow": "OverflowComplex",
	},
	"*reflect.FloatValue": {
		"Get":      "Float",
		"Set":      "SetFloat",
		"Overflow": "OverflowFloat",
	},
	"*reflect.FuncValue": {
		"Get": "Pointer",
	},
	"*reflect.IntValue": {
		"Get":      "Int",
		"Set":      "SetInt",
		"Overflow": "OverflowInt",
	},
	"*reflect.InterfaceValue": {
		"Get": "InterfaceData",
	},
	"*reflect.MapValue": {
		"Elem":    "MapIndex",
		"Get":     "Pointer",
		"Keys":    "MapKeys",
		"SetElem": "SetMapIndex",
	},
	"*reflect.PtrValue": {
		"Get": "Pointer",
	},
	"*reflect.SliceValue": {
		"Elem": "Index",
		"Get":  "Pointer",
	},
	"*reflect.StringValue": {
		"Get": "String",
		"Set": "SetString",
	},
	"*reflect.UintValue": {
		"Get":      "Uint",
		"Set":      "SetUint",
		"Overflow": "OverflowUint",
	},
	"*reflect.UnsafePointerValue": {
		"Get": "Pointer",
		"Set": "SetPointer",
	},
}

var reflectKind = map[string][]string{
	"reflect.ArrayOrSliceType":   {"Array", "Slice"}, // interface, not pointer
	"*reflect.ArrayType":         {"Array"},
	"*reflect.BoolType":          {"Bool"},
	"*reflect.ChanType":          {"Chan"},
	"*reflect.ComplexType":       {"Complex64", "Complex128"},
	"*reflect.FloatType":         {"Float32", "Float64"},
	"*reflect.FuncType":          {"Func"},
	"*reflect.IntType":           {"Int", "Int8", "Int16", "Int32", "Int64"},
	"*reflect.InterfaceType":     {"Interface"},
	"*reflect.MapType":           {"Map"},
	"*reflect.PtrType":           {"Ptr"},
	"*reflect.SliceType":         {"Slice"},
	"*reflect.StringType":        {"String"},
	"*reflect.StructType":        {"Struct"},
	"*reflect.UintType":          {"Uint", "Uint8", "Uint16", "Uint32", "Uint64", "Uintptr"},
	"*reflect.UnsafePointerType": {"UnsafePointer"},

	"reflect.ArrayOrSliceValue":   {"Array", "Slice"}, // interface, not pointer
	"*reflect.ArrayValue":         {"Array"},
	"*reflect.BoolValue":          {"Bool"},
	"*reflect.ChanValue":          {"Chan"},
	"*reflect.ComplexValue":       {"Complex64", "Complex128"},
	"*reflect.FloatValue":         {"Float32", "Float64"},
	"*reflect.FuncValue":          {"Func"},
	"*reflect.IntValue":           {"Int", "Int8", "Int16", "Int32", "Int64"},
	"*reflect.InterfaceValue":     {"Interface"},
	"*reflect.MapValue":           {"Map"},
	"*reflect.PtrValue":           {"Ptr"},
	"*reflect.SliceValue":         {"Slice"},
	"*reflect.StringValue":        {"String"},
	"*reflect.StructValue":        {"Struct"},
	"*reflect.UintValue":          {"Uint", "Uint8", "Uint16", "Uint32", "Uint64", "Uintptr"},
	"*reflect.UnsafePointerValue": {"UnsafePointer"},
}

var isReflectValue = map[string]bool{
	"reflect.ArrayOrSliceValue":   true, // interface, not pointer
	"*reflect.ArrayValue":         true,
	"*reflect.BoolValue":          true,
	"*reflect.ChanValue":          true,
	"*reflect.ComplexValue":       true,
	"*reflect.FloatValue":         true,
	"*reflect.FuncValue":          true,
	"*reflect.IntValue":           true,
	"*reflect.InterfaceValue":     true,
	"*reflect.MapValue":           true,
	"*reflect.PtrValue":           true,
	"*reflect.SliceValue":         true,
	"*reflect.StringValue":        true,
	"*reflect.StructValue":        true,
	"*reflect.UintValue":          true,
	"*reflect.UnsafePointerValue": true,
	"reflect.Value":               true, // interface, not pointer
}