1// Package astcopy implements Go AST reflection-free deep copy operations.
2package astcopy
3
4import (
5	"go/ast"
6)
7
8// Node returns x node deep copy.
9// Copy of nil argument is nil.
10func Node(x ast.Node) ast.Node {
11	return copyNode(x)
12}
13
14// NodeList returns xs node slice deep copy.
15// Copy of nil argument is nil.
16func NodeList(xs []ast.Node) []ast.Node {
17	if xs == nil {
18		return nil
19	}
20	cp := make([]ast.Node, len(xs))
21	for i := range xs {
22		cp[i] = copyNode(xs[i])
23	}
24	return cp
25}
26
27// Expr returns x expression deep copy.
28// Copy of nil argument is nil.
29func Expr(x ast.Expr) ast.Expr {
30	return copyExpr(x)
31}
32
33// ExprList returns xs expression slice deep copy.
34// Copy of nil argument is nil.
35func ExprList(xs []ast.Expr) []ast.Expr {
36	if xs == nil {
37		return nil
38	}
39	cp := make([]ast.Expr, len(xs))
40	for i := range xs {
41		cp[i] = copyExpr(xs[i])
42	}
43	return cp
44}
45
46// Stmt returns x statement deep copy.
47// Copy of nil argument is nil.
48func Stmt(x ast.Stmt) ast.Stmt {
49	return copyStmt(x)
50}
51
52// StmtList returns xs statement slice deep copy.
53// Copy of nil argument is nil.
54func StmtList(xs []ast.Stmt) []ast.Stmt {
55	if xs == nil {
56		return nil
57	}
58	cp := make([]ast.Stmt, len(xs))
59	for i := range xs {
60		cp[i] = copyStmt(xs[i])
61	}
62	return cp
63}
64
65// Decl returns x declaration deep copy.
66// Copy of nil argument is nil.
67func Decl(x ast.Decl) ast.Decl {
68	return copyDecl(x)
69}
70
71// DeclList returns xs declaration slice deep copy.
72// Copy of nil argument is nil.
73func DeclList(xs []ast.Decl) []ast.Decl {
74	if xs == nil {
75		return nil
76	}
77	cp := make([]ast.Decl, len(xs))
78	for i := range xs {
79		cp[i] = copyDecl(xs[i])
80	}
81	return cp
82}
83
84// BadExpr returns x deep copy.
85// Copy of nil argument is nil.
86func BadExpr(x *ast.BadExpr) *ast.BadExpr {
87	if x == nil {
88		return nil
89	}
90	cp := *x
91	return &cp
92}
93
94// Ident returns x deep copy.
95// Copy of nil argument is nil.
96func Ident(x *ast.Ident) *ast.Ident {
97	if x == nil {
98		return nil
99	}
100	cp := *x
101	return &cp
102}
103
104// IdentList returns xs identifier slice deep copy.
105// Copy of nil argument is nil.
106func IdentList(xs []*ast.Ident) []*ast.Ident {
107	if xs == nil {
108		return nil
109	}
110	cp := make([]*ast.Ident, len(xs))
111	for i := range xs {
112		cp[i] = Ident(xs[i])
113	}
114	return cp
115}
116
117// Ellipsis returns x deep copy.
118// Copy of nil argument is nil.
119func Ellipsis(x *ast.Ellipsis) *ast.Ellipsis {
120	if x == nil {
121		return nil
122	}
123	cp := *x
124	cp.Elt = copyExpr(x.Elt)
125	return &cp
126}
127
128// BasicLit returns x deep copy.
129// Copy of nil argument is nil.
130func BasicLit(x *ast.BasicLit) *ast.BasicLit {
131	if x == nil {
132		return nil
133	}
134	cp := *x
135	return &cp
136}
137
138// FuncLit returns x deep copy.
139// Copy of nil argument is nil.
140func FuncLit(x *ast.FuncLit) *ast.FuncLit {
141	if x == nil {
142		return nil
143	}
144	cp := *x
145	cp.Type = FuncType(x.Type)
146	cp.Body = BlockStmt(x.Body)
147	return &cp
148}
149
150// CompositeLit returns x deep copy.
151// Copy of nil argument is nil.
152func CompositeLit(x *ast.CompositeLit) *ast.CompositeLit {
153	if x == nil {
154		return nil
155	}
156	cp := *x
157	cp.Type = copyExpr(x.Type)
158	cp.Elts = ExprList(x.Elts)
159	return &cp
160}
161
162// ParenExpr returns x deep copy.
163// Copy of nil argument is nil.
164func ParenExpr(x *ast.ParenExpr) *ast.ParenExpr {
165	if x == nil {
166		return nil
167	}
168	cp := *x
169	cp.X = copyExpr(x.X)
170	return &cp
171}
172
173// SelectorExpr returns x deep copy.
174// Copy of nil argument is nil.
175func SelectorExpr(x *ast.SelectorExpr) *ast.SelectorExpr {
176	if x == nil {
177		return nil
178	}
179	cp := *x
180	cp.X = copyExpr(x.X)
181	cp.Sel = Ident(x.Sel)
182	return &cp
183}
184
185// IndexExpr returns x deep copy.
186// Copy of nil argument is nil.
187func IndexExpr(x *ast.IndexExpr) *ast.IndexExpr {
188	if x == nil {
189		return nil
190	}
191	cp := *x
192	cp.X = copyExpr(x.X)
193	cp.Index = copyExpr(x.Index)
194	return &cp
195}
196
197// SliceExpr returns x deep copy.
198// Copy of nil argument is nil.
199func SliceExpr(x *ast.SliceExpr) *ast.SliceExpr {
200	if x == nil {
201		return nil
202	}
203	cp := *x
204	cp.X = copyExpr(x.X)
205	cp.Low = copyExpr(x.Low)
206	cp.High = copyExpr(x.High)
207	cp.Max = copyExpr(x.Max)
208	return &cp
209}
210
211// TypeAssertExpr returns x deep copy.
212// Copy of nil argument is nil.
213func TypeAssertExpr(x *ast.TypeAssertExpr) *ast.TypeAssertExpr {
214	if x == nil {
215		return nil
216	}
217	cp := *x
218	cp.X = copyExpr(x.X)
219	cp.Type = copyExpr(x.Type)
220	return &cp
221}
222
223// CallExpr returns x deep copy.
224// Copy of nil argument is nil.
225func CallExpr(x *ast.CallExpr) *ast.CallExpr {
226	if x == nil {
227		return nil
228	}
229	cp := *x
230	cp.Fun = copyExpr(x.Fun)
231	cp.Args = ExprList(x.Args)
232	return &cp
233}
234
235// StarExpr returns x deep copy.
236// Copy of nil argument is nil.
237func StarExpr(x *ast.StarExpr) *ast.StarExpr {
238	if x == nil {
239		return nil
240	}
241	cp := *x
242	cp.X = copyExpr(x.X)
243	return &cp
244}
245
246// UnaryExpr returns x deep copy.
247// Copy of nil argument is nil.
248func UnaryExpr(x *ast.UnaryExpr) *ast.UnaryExpr {
249	if x == nil {
250		return nil
251	}
252	cp := *x
253	cp.X = copyExpr(x.X)
254	return &cp
255}
256
257// BinaryExpr returns x deep copy.
258// Copy of nil argument is nil.
259func BinaryExpr(x *ast.BinaryExpr) *ast.BinaryExpr {
260	if x == nil {
261		return nil
262	}
263	cp := *x
264	cp.X = copyExpr(x.X)
265	cp.Y = copyExpr(x.Y)
266	return &cp
267}
268
269// KeyValueExpr returns x deep copy.
270// Copy of nil argument is nil.
271func KeyValueExpr(x *ast.KeyValueExpr) *ast.KeyValueExpr {
272	if x == nil {
273		return nil
274	}
275	cp := *x
276	cp.Key = copyExpr(x.Key)
277	cp.Value = copyExpr(x.Value)
278	return &cp
279}
280
281// ArrayType returns x deep copy.
282// Copy of nil argument is nil.
283func ArrayType(x *ast.ArrayType) *ast.ArrayType {
284	if x == nil {
285		return nil
286	}
287	cp := *x
288	cp.Len = copyExpr(x.Len)
289	cp.Elt = copyExpr(x.Elt)
290	return &cp
291}
292
293// StructType returns x deep copy.
294// Copy of nil argument is nil.
295func StructType(x *ast.StructType) *ast.StructType {
296	if x == nil {
297		return nil
298	}
299	cp := *x
300	cp.Fields = FieldList(x.Fields)
301	return &cp
302}
303
304// Field returns x deep copy.
305// Copy of nil argument is nil.
306func Field(x *ast.Field) *ast.Field {
307	if x == nil {
308		return nil
309	}
310	cp := *x
311	cp.Names = IdentList(x.Names)
312	cp.Type = copyExpr(x.Type)
313	cp.Tag = BasicLit(x.Tag)
314	cp.Doc = CommentGroup(x.Doc)
315	cp.Comment = CommentGroup(x.Comment)
316	return &cp
317}
318
319// FieldList returns x deep copy.
320// Copy of nil argument is nil.
321func FieldList(x *ast.FieldList) *ast.FieldList {
322	if x == nil {
323		return nil
324	}
325	cp := *x
326	if x.List != nil {
327		cp.List = make([]*ast.Field, len(x.List))
328		for i := range x.List {
329			cp.List[i] = Field(x.List[i])
330		}
331	}
332	return &cp
333}
334
335// FuncType returns x deep copy.
336// Copy of nil argument is nil.
337func FuncType(x *ast.FuncType) *ast.FuncType {
338	if x == nil {
339		return nil
340	}
341	cp := *x
342	cp.Params = FieldList(x.Params)
343	cp.Results = FieldList(x.Results)
344	return &cp
345}
346
347// InterfaceType returns x deep copy.
348// Copy of nil argument is nil.
349func InterfaceType(x *ast.InterfaceType) *ast.InterfaceType {
350	if x == nil {
351		return nil
352	}
353	cp := *x
354	cp.Methods = FieldList(x.Methods)
355	return &cp
356}
357
358// MapType returns x deep copy.
359// Copy of nil argument is nil.
360func MapType(x *ast.MapType) *ast.MapType {
361	if x == nil {
362		return nil
363	}
364	cp := *x
365	cp.Key = copyExpr(x.Key)
366	cp.Value = copyExpr(x.Value)
367	return &cp
368}
369
370// ChanType returns x deep copy.
371// Copy of nil argument is nil.
372func ChanType(x *ast.ChanType) *ast.ChanType {
373	if x == nil {
374		return nil
375	}
376	cp := *x
377	cp.Value = copyExpr(x.Value)
378	return &cp
379}
380
381// BlockStmt returns x deep copy.
382// Copy of nil argument is nil.
383func BlockStmt(x *ast.BlockStmt) *ast.BlockStmt {
384	if x == nil {
385		return nil
386	}
387	cp := *x
388	cp.List = StmtList(x.List)
389	return &cp
390}
391
392// ImportSpec returns x deep copy.
393// Copy of nil argument is nil.
394func ImportSpec(x *ast.ImportSpec) *ast.ImportSpec {
395	if x == nil {
396		return nil
397	}
398	cp := *x
399	cp.Name = Ident(x.Name)
400	cp.Path = BasicLit(x.Path)
401	cp.Doc = CommentGroup(x.Doc)
402	cp.Comment = CommentGroup(x.Comment)
403	return &cp
404}
405
406// ValueSpec returns x deep copy.
407// Copy of nil argument is nil.
408func ValueSpec(x *ast.ValueSpec) *ast.ValueSpec {
409	if x == nil {
410		return nil
411	}
412	cp := *x
413	cp.Names = IdentList(x.Names)
414	cp.Values = ExprList(x.Values)
415	cp.Type = copyExpr(x.Type)
416	cp.Doc = CommentGroup(x.Doc)
417	cp.Comment = CommentGroup(x.Comment)
418	return &cp
419}
420
421// TypeSpec returns x deep copy.
422// Copy of nil argument is nil.
423func TypeSpec(x *ast.TypeSpec) *ast.TypeSpec {
424	if x == nil {
425		return nil
426	}
427	cp := *x
428	cp.Name = Ident(x.Name)
429	cp.Type = copyExpr(x.Type)
430	cp.Doc = CommentGroup(x.Doc)
431	cp.Comment = CommentGroup(x.Comment)
432	return &cp
433}
434
435// Spec returns x deep copy.
436// Copy of nil argument is nil.
437func Spec(x ast.Spec) ast.Spec {
438	if x == nil {
439		return nil
440	}
441
442	switch x := x.(type) {
443	case *ast.ImportSpec:
444		return ImportSpec(x)
445	case *ast.ValueSpec:
446		return ValueSpec(x)
447	case *ast.TypeSpec:
448		return TypeSpec(x)
449	default:
450		panic("unhandled spec")
451	}
452}
453
454// SpecList returns xs spec slice deep copy.
455// Copy of nil argument is nil.
456func SpecList(xs []ast.Spec) []ast.Spec {
457	if xs == nil {
458		return nil
459	}
460	cp := make([]ast.Spec, len(xs))
461	for i := range xs {
462		cp[i] = Spec(xs[i])
463	}
464	return cp
465}
466
467// BadStmt returns x deep copy.
468// Copy of nil argument is nil.
469func BadStmt(x *ast.BadStmt) *ast.BadStmt {
470	if x == nil {
471		return nil
472	}
473	cp := *x
474	return &cp
475}
476
477// DeclStmt returns x deep copy.
478// Copy of nil argument is nil.
479func DeclStmt(x *ast.DeclStmt) *ast.DeclStmt {
480	if x == nil {
481		return nil
482	}
483	cp := *x
484	cp.Decl = copyDecl(x.Decl)
485	return &cp
486}
487
488// EmptyStmt returns x deep copy.
489// Copy of nil argument is nil.
490func EmptyStmt(x *ast.EmptyStmt) *ast.EmptyStmt {
491	if x == nil {
492		return nil
493	}
494	cp := *x
495	return &cp
496}
497
498// LabeledStmt returns x deep copy.
499// Copy of nil argument is nil.
500func LabeledStmt(x *ast.LabeledStmt) *ast.LabeledStmt {
501	if x == nil {
502		return nil
503	}
504	cp := *x
505	cp.Label = Ident(x.Label)
506	cp.Stmt = copyStmt(x.Stmt)
507	return &cp
508}
509
510// ExprStmt returns x deep copy.
511// Copy of nil argument is nil.
512func ExprStmt(x *ast.ExprStmt) *ast.ExprStmt {
513	if x == nil {
514		return nil
515	}
516	cp := *x
517	cp.X = copyExpr(x.X)
518	return &cp
519}
520
521// SendStmt returns x deep copy.
522// Copy of nil argument is nil.
523func SendStmt(x *ast.SendStmt) *ast.SendStmt {
524	if x == nil {
525		return nil
526	}
527	cp := *x
528	cp.Chan = copyExpr(x.Chan)
529	cp.Value = copyExpr(x.Value)
530	return &cp
531}
532
533// IncDecStmt returns x deep copy.
534// Copy of nil argument is nil.
535func IncDecStmt(x *ast.IncDecStmt) *ast.IncDecStmt {
536	if x == nil {
537		return nil
538	}
539	cp := *x
540	cp.X = copyExpr(x.X)
541	return &cp
542}
543
544// AssignStmt returns x deep copy.
545// Copy of nil argument is nil.
546func AssignStmt(x *ast.AssignStmt) *ast.AssignStmt {
547	if x == nil {
548		return nil
549	}
550	cp := *x
551	cp.Lhs = ExprList(x.Lhs)
552	cp.Rhs = ExprList(x.Rhs)
553	return &cp
554}
555
556// GoStmt returns x deep copy.
557// Copy of nil argument is nil.
558func GoStmt(x *ast.GoStmt) *ast.GoStmt {
559	if x == nil {
560		return nil
561	}
562	cp := *x
563	cp.Call = CallExpr(x.Call)
564	return &cp
565}
566
567// DeferStmt returns x deep copy.
568// Copy of nil argument is nil.
569func DeferStmt(x *ast.DeferStmt) *ast.DeferStmt {
570	if x == nil {
571		return nil
572	}
573	cp := *x
574	cp.Call = CallExpr(x.Call)
575	return &cp
576}
577
578// ReturnStmt returns x deep copy.
579// Copy of nil argument is nil.
580func ReturnStmt(x *ast.ReturnStmt) *ast.ReturnStmt {
581	if x == nil {
582		return nil
583	}
584	cp := *x
585	cp.Results = ExprList(x.Results)
586	return &cp
587}
588
589// BranchStmt returns x deep copy.
590// Copy of nil argument is nil.
591func BranchStmt(x *ast.BranchStmt) *ast.BranchStmt {
592	if x == nil {
593		return nil
594	}
595	cp := *x
596	cp.Label = Ident(x.Label)
597	return &cp
598}
599
600// IfStmt returns x deep copy.
601// Copy of nil argument is nil.
602func IfStmt(x *ast.IfStmt) *ast.IfStmt {
603	if x == nil {
604		return nil
605	}
606	cp := *x
607	cp.Init = copyStmt(x.Init)
608	cp.Cond = copyExpr(x.Cond)
609	cp.Body = BlockStmt(x.Body)
610	cp.Else = copyStmt(x.Else)
611	return &cp
612}
613
614// CaseClause returns x deep copy.
615// Copy of nil argument is nil.
616func CaseClause(x *ast.CaseClause) *ast.CaseClause {
617	if x == nil {
618		return nil
619	}
620	cp := *x
621	cp.List = ExprList(x.List)
622	cp.Body = StmtList(x.Body)
623	return &cp
624}
625
626// SwitchStmt returns x deep copy.
627// Copy of nil argument is nil.
628func SwitchStmt(x *ast.SwitchStmt) *ast.SwitchStmt {
629	if x == nil {
630		return nil
631	}
632	cp := *x
633	cp.Init = copyStmt(x.Init)
634	cp.Tag = copyExpr(x.Tag)
635	cp.Body = BlockStmt(x.Body)
636	return &cp
637}
638
639// TypeSwitchStmt returns x deep copy.
640// Copy of nil argument is nil.
641func TypeSwitchStmt(x *ast.TypeSwitchStmt) *ast.TypeSwitchStmt {
642	if x == nil {
643		return nil
644	}
645	cp := *x
646	cp.Init = copyStmt(x.Init)
647	cp.Assign = copyStmt(x.Assign)
648	cp.Body = BlockStmt(x.Body)
649	return &cp
650}
651
652// CommClause returns x deep copy.
653// Copy of nil argument is nil.
654func CommClause(x *ast.CommClause) *ast.CommClause {
655	if x == nil {
656		return nil
657	}
658	cp := *x
659	cp.Comm = copyStmt(x.Comm)
660	cp.Body = StmtList(x.Body)
661	return &cp
662}
663
664// SelectStmt returns x deep copy.
665// Copy of nil argument is nil.
666func SelectStmt(x *ast.SelectStmt) *ast.SelectStmt {
667	if x == nil {
668		return nil
669	}
670	cp := *x
671	cp.Body = BlockStmt(x.Body)
672	return &cp
673}
674
675// ForStmt returns x deep copy.
676// Copy of nil argument is nil.
677func ForStmt(x *ast.ForStmt) *ast.ForStmt {
678	if x == nil {
679		return nil
680	}
681	cp := *x
682	cp.Init = copyStmt(x.Init)
683	cp.Cond = copyExpr(x.Cond)
684	cp.Post = copyStmt(x.Post)
685	cp.Body = BlockStmt(x.Body)
686	return &cp
687}
688
689// RangeStmt returns x deep copy.
690// Copy of nil argument is nil.
691func RangeStmt(x *ast.RangeStmt) *ast.RangeStmt {
692	if x == nil {
693		return nil
694	}
695	cp := *x
696	cp.Key = copyExpr(x.Key)
697	cp.Value = copyExpr(x.Value)
698	cp.X = copyExpr(x.X)
699	cp.Body = BlockStmt(x.Body)
700	return &cp
701}
702
703// Comment returns x deep copy.
704// Copy of nil argument is nil.
705func Comment(x *ast.Comment) *ast.Comment {
706	if x == nil {
707		return nil
708	}
709	cp := *x
710	return &cp
711}
712
713// CommentGroup returns x deep copy.
714// Copy of nil argument is nil.
715func CommentGroup(x *ast.CommentGroup) *ast.CommentGroup {
716	if x == nil {
717		return nil
718	}
719	cp := *x
720	if x.List != nil {
721		cp.List = make([]*ast.Comment, len(x.List))
722		for i := range x.List {
723			cp.List[i] = Comment(x.List[i])
724		}
725	}
726	return &cp
727}
728
729// File returns x deep copy.
730// Copy of nil argument is nil.
731func File(x *ast.File) *ast.File {
732	if x == nil {
733		return nil
734	}
735	cp := *x
736	cp.Doc = CommentGroup(x.Doc)
737	cp.Name = Ident(x.Name)
738	cp.Decls = DeclList(x.Decls)
739	cp.Imports = make([]*ast.ImportSpec, len(x.Imports))
740	for i := range x.Imports {
741		cp.Imports[i] = ImportSpec(x.Imports[i])
742	}
743	cp.Unresolved = IdentList(x.Unresolved)
744	cp.Comments = make([]*ast.CommentGroup, len(x.Comments))
745	for i := range x.Comments {
746		cp.Comments[i] = CommentGroup(x.Comments[i])
747	}
748	return &cp
749}
750
751// Package returns x deep copy.
752// Copy of nil argument is nil.
753func Package(x *ast.Package) *ast.Package {
754	if x == nil {
755		return nil
756	}
757	cp := *x
758	cp.Files = make(map[string]*ast.File)
759	for filename, f := range x.Files {
760		cp.Files[filename] = f
761	}
762	return &cp
763}
764
765// BadDecl returns x deep copy.
766// Copy of nil argument is nil.
767func BadDecl(x *ast.BadDecl) *ast.BadDecl {
768	if x == nil {
769		return nil
770	}
771	cp := *x
772	return &cp
773}
774
775// GenDecl returns x deep copy.
776// Copy of nil argument is nil.
777func GenDecl(x *ast.GenDecl) *ast.GenDecl {
778	if x == nil {
779		return nil
780	}
781	cp := *x
782	cp.Specs = SpecList(x.Specs)
783	cp.Doc = CommentGroup(x.Doc)
784	return &cp
785}
786
787// FuncDecl returns x deep copy.
788// Copy of nil argument is nil.
789func FuncDecl(x *ast.FuncDecl) *ast.FuncDecl {
790	if x == nil {
791		return nil
792	}
793	cp := *x
794	cp.Recv = FieldList(x.Recv)
795	cp.Name = Ident(x.Name)
796	cp.Type = FuncType(x.Type)
797	cp.Body = BlockStmt(x.Body)
798	cp.Doc = CommentGroup(x.Doc)
799	return &cp
800}
801
802func copyNode(x ast.Node) ast.Node {
803	switch x := x.(type) {
804	case ast.Expr:
805		return copyExpr(x)
806	case ast.Stmt:
807		return copyStmt(x)
808	case ast.Decl:
809		return copyDecl(x)
810
811	case ast.Spec:
812		return Spec(x)
813	case *ast.FieldList:
814		return FieldList(x)
815	case *ast.Comment:
816		return Comment(x)
817	case *ast.CommentGroup:
818		return CommentGroup(x)
819	case *ast.File:
820		return File(x)
821	case *ast.Package:
822		return Package(x)
823
824	default:
825		panic("unhandled node")
826	}
827}
828
829func copyExpr(x ast.Expr) ast.Expr {
830	if x == nil {
831		return nil
832	}
833
834	switch x := x.(type) {
835	case *ast.BadExpr:
836		return BadExpr(x)
837	case *ast.Ident:
838		return Ident(x)
839	case *ast.Ellipsis:
840		return Ellipsis(x)
841	case *ast.BasicLit:
842		return BasicLit(x)
843	case *ast.FuncLit:
844		return FuncLit(x)
845	case *ast.CompositeLit:
846		return CompositeLit(x)
847	case *ast.ParenExpr:
848		return ParenExpr(x)
849	case *ast.SelectorExpr:
850		return SelectorExpr(x)
851	case *ast.IndexExpr:
852		return IndexExpr(x)
853	case *ast.SliceExpr:
854		return SliceExpr(x)
855	case *ast.TypeAssertExpr:
856		return TypeAssertExpr(x)
857	case *ast.CallExpr:
858		return CallExpr(x)
859	case *ast.StarExpr:
860		return StarExpr(x)
861	case *ast.UnaryExpr:
862		return UnaryExpr(x)
863	case *ast.BinaryExpr:
864		return BinaryExpr(x)
865	case *ast.KeyValueExpr:
866		return KeyValueExpr(x)
867	case *ast.ArrayType:
868		return ArrayType(x)
869	case *ast.StructType:
870		return StructType(x)
871	case *ast.FuncType:
872		return FuncType(x)
873	case *ast.InterfaceType:
874		return InterfaceType(x)
875	case *ast.MapType:
876		return MapType(x)
877	case *ast.ChanType:
878		return ChanType(x)
879
880	default:
881		panic("unhandled expr")
882	}
883}
884
885func copyStmt(x ast.Stmt) ast.Stmt {
886	if x == nil {
887		return nil
888	}
889
890	switch x := x.(type) {
891	case *ast.BadStmt:
892		return BadStmt(x)
893	case *ast.DeclStmt:
894		return DeclStmt(x)
895	case *ast.EmptyStmt:
896		return EmptyStmt(x)
897	case *ast.LabeledStmt:
898		return LabeledStmt(x)
899	case *ast.ExprStmt:
900		return ExprStmt(x)
901	case *ast.SendStmt:
902		return SendStmt(x)
903	case *ast.IncDecStmt:
904		return IncDecStmt(x)
905	case *ast.AssignStmt:
906		return AssignStmt(x)
907	case *ast.GoStmt:
908		return GoStmt(x)
909	case *ast.DeferStmt:
910		return DeferStmt(x)
911	case *ast.ReturnStmt:
912		return ReturnStmt(x)
913	case *ast.BranchStmt:
914		return BranchStmt(x)
915	case *ast.BlockStmt:
916		return BlockStmt(x)
917	case *ast.IfStmt:
918		return IfStmt(x)
919	case *ast.CaseClause:
920		return CaseClause(x)
921	case *ast.SwitchStmt:
922		return SwitchStmt(x)
923	case *ast.TypeSwitchStmt:
924		return TypeSwitchStmt(x)
925	case *ast.CommClause:
926		return CommClause(x)
927	case *ast.SelectStmt:
928		return SelectStmt(x)
929	case *ast.ForStmt:
930		return ForStmt(x)
931	case *ast.RangeStmt:
932		return RangeStmt(x)
933
934	default:
935		panic("unhandled stmt")
936	}
937}
938
939func copyDecl(x ast.Decl) ast.Decl {
940	if x == nil {
941		return nil
942	}
943
944	switch x := x.(type) {
945	case *ast.BadDecl:
946		return BadDecl(x)
947	case *ast.GenDecl:
948		return GenDecl(x)
949	case *ast.FuncDecl:
950		return FuncDecl(x)
951
952	default:
953		panic("unhandled decl")
954	}
955}
956