1 package loader
2 3 import (
4 "go/ast"
5 "go/token"
6 "go/types"
7 8 "golang.org/x/tools/go/ast/astutil"
9 )
10 11 // rewriteValueReceiverRefs makes a value receiver over a NAMED slice or map
12 // behave as a reference to the caller's variable, matching stage4's front end.
13 //
14 // The legacy compiler gets there through pointer receivers: the receiver type
15 // becomes *T and every USE of the receiver name in the body becomes (*name).
16 // That is the same code pointer-receiver methods already generate, so the
17 // sovereign-arena prologue, the receiver store-back and the method-set rules
18 // need no separate path. Go's addressability requirement is already dropped by
19 // the patched type checker (a pointer-receiver method is callable on a value
20 // expression and belongs to the value type's method set), so a call site keeps
21 // working unchanged.
22 //
23 // The rewrite is driven by the FIRST type-check pass: info.Uses tells us
24 // exactly which identifiers refer to the receiver variable, so a declaration
25 // that shadows the name inside the body is left alone. The caller must
26 // re-typecheck afterwards; Check() already has that path for the pipe/byte
27 // rewrites.
28 func rewriteValueReceiverRefs(files []*ast.File, info *types.Info) (n int) {
29 if info == nil || len(info.Uses) == 0 {
30 return 0
31 }
32 aggs := map[string]bool{}
33 for _, f := range files {
34 for _, d := range f.Decls {
35 gd, ok := d.(*ast.GenDecl)
36 if !ok || gd.Tok != token.TYPE {
37 continue
38 }
39 for _, sp := range gd.Specs {
40 ts, ok := sp.(*ast.TypeSpec)
41 if !ok || ts.Name == nil || ts.Assign.IsValid() {
42 continue
43 }
44 switch t := ts.Type.(type) {
45 case *ast.ArrayType:
46 if t.Len == nil {
47 aggs[ts.Name.Name] = true
48 }
49 case *ast.MapType:
50 aggs[ts.Name.Name] = true
51 }
52 }
53 }
54 }
55 if len(aggs) == 0 {
56 return 0
57 }
58 for _, f := range files {
59 for _, d := range f.Decls {
60 fd, ok := d.(*ast.FuncDecl)
61 if !ok || fd.Recv == nil || len(fd.Recv.List) == 0 || fd.Body == nil {
62 continue
63 }
64 rf := fd.Recv.List[0]
65 if len(rf.Names) == 0 {
66 continue
67 }
68 name := rf.Names[0].Name
69 if name == "" || name == "_" {
70 continue
71 }
72 if _, isStar := rf.Type.(*ast.StarExpr); isStar {
73 continue
74 }
75 if !aggs[recvTypeIdent(rf.Type)] {
76 continue
77 }
78 obj := info.Defs[rf.Names[0]]
79 if obj == nil {
80 continue
81 }
82 rf.Type = &ast.StarExpr{X: rf.Type}
83 fd.Body = rewriteRecvUses(fd.Body, name, obj, info)
84 n++
85 }
86 }
87 return n
88 }
89 90 // recvTypeIdent returns the named type a receiver's type expression names:
91 // T, T[P], or T[P, Q].
92 func recvTypeIdent(t ast.Expr) (s string) {
93 switch x := t.(type) {
94 case *ast.Ident:
95 return x.Name
96 case *ast.IndexExpr:
97 if id, ok := x.X.(*ast.Ident); ok {
98 return id.Name
99 }
100 case *ast.IndexListExpr:
101 if id, ok := x.X.(*ast.Ident); ok {
102 return id.Name
103 }
104 }
105 return ""
106 }
107 108 // rewriteRecvUses replaces every use of the receiver variable in body with
109 // (*name). Uses are matched by types.Object identity, so a shadowing
110 // declaration of the same name is not touched.
111 func rewriteRecvUses(body *ast.BlockStmt, name string, obj types.Object, info *types.Info) *ast.BlockStmt {
112 out := astutil.Apply(body, func(c *astutil.Cursor) bool {
113 id, ok := c.Node().(*ast.Ident)
114 if !ok || id.Name != name || info.Uses[id] != obj {
115 return true
116 }
117 c.Replace(&ast.ParenExpr{X: &ast.StarExpr{X: ast.NewIdent(name)}})
118 return false
119 }, nil)
120 blk, _ := out.(*ast.BlockStmt)
121 return blk
122 }
123