1 package main
2 3 import (
4 "moxielang.dev/pkg/syntax"
5 "moxielang.dev/pkg/token"
6 )
7 8 // edgeAffinityCtx enforces D19 rule 4 on one function body: a declared pair owns
9 // one pair of rings and one pair of handles, so it has one owner and MOVES
10 // ONCE - into a spawn, or out of the function as a return value. Reading its
11 // fields (e.Send / e.Recv) is a read, not a copy. Everything else is a copy:
12 // `e2 := e1`, `f(e1)` for a non-spawn f, a struct literal field, a send, a slice
13 // element. A second spawn would give two domains the same handles.
14 //
15 // The walk judges an expression by its TYPE rather than by tracking declared
16 // objects. The declaration is compiler-generated ("var p Edge[...]"), and every
17 // expression carries the checker's TypeAndValue, so "is this value an Edge"
18 // answers the question directly and shadowing cannot confuse it.
19 type edgeAffinityCtx struct {
20 // vars are the names this body declares as a pair: the lowered declaration
21 // is `var p Edge[In, Out]`, and Edge is a universe generic, so the shape is
22 // syntactic and needs no type information.
23 vars map[string]bool
24 moved map[string]bool
25 msg string
26 }
27 28 func (c *edgeAffinityCtx) fail(m string) {
29 if c.msg == "" {
30 c.msg = m
31 }
32 }
33 34 // edgeAffinityFile runs the check over every function body in one file. The
35 // first violation goes into the deferred-error table, which CompileToIR turns
36 // into a build failure: the typecheck pass drops its own compileErrors on
37 // purpose (their slices live in the pass arena).
38 func edgeAffinityFile(f *syntax.File, pkgName string) {
39 if f == nil {
40 return
41 }
42 for i := int32(0); i < int32(len(f.DeclList)); i++ {
43 fd, ok := f.DeclList[i].(*syntax.FuncDecl)
44 if !ok || fd == nil || fd.Body == nil {
45 continue
46 }
47 c := &edgeAffinityCtx{vars: map[string]bool{}, moved: map[string]bool{}}
48 c.collectFields(fd.Type.ParamList)
49 c.collectFields(fd.Type.ResultList)
50 c.collectStmts(fd.Body.List)
51 if len(c.vars) == 0 {
52 continue
53 }
54 c.stmts(fd.Body.List)
55 if c.msg != "" {
56 reportConstOverflow(pkgName, c.msg)
57 return
58 }
59 }
60 }
61 62 func (c *edgeAffinityCtx) collectFields(list []*syntax.Field) {
63 for i := int32(0); i < int32(len(list)); i++ {
64 f := list[i]
65 if f == nil || f.Name == nil || f.Type == nil {
66 continue
67 }
68 if c.isEdgeTypeExpr(f.Type) {
69 c.vars[f.Name.Value] = true
70 }
71 }
72 }
73 74 func (c *edgeAffinityCtx) collectStmts(list []syntax.Stmt) {
75 for i := int32(0); i < int32(len(list)); i++ {
76 switch s := list[i].(type) {
77 case *syntax.BlockStmt:
78 c.collectStmts(s.List)
79 case *syntax.LabeledStmt:
80 c.collectStmts([]syntax.Stmt{s.Stmt})
81 case *syntax.DeclStmt:
82 for j := int32(0); j < int32(len(s.DeclList)); j++ {
83 vd, ok := s.DeclList[j].(*syntax.VarDecl)
84 if !ok || vd.Type == nil {
85 continue
86 }
87 if !c.isEdgeTypeExpr(vd.Type) {
88 continue
89 }
90 for k := int32(0); k < int32(len(vd.NameList)); k++ {
91 c.vars[vd.NameList[k].Value] = true
92 }
93 }
94 case *syntax.IfStmt:
95 c.collectStmts([]syntax.Stmt{s.Then})
96 if s.Else != nil {
97 c.collectStmts([]syntax.Stmt{s.Else})
98 }
99 case *syntax.ForStmt:
100 c.collectStmts([]syntax.Stmt{s.Body})
101 case *syntax.SwitchStmt:
102 for j := int32(0); j < int32(len(s.Body)); j++ {
103 c.collectStmts(s.Body[j].Body)
104 }
105 case *syntax.SelectStmt:
106 for j := int32(0); j < int32(len(s.Body)); j++ {
107 c.collectStmts(s.Body[j].Body)
108 }
109 }
110 }
111 }
112 113 // isEdgeTypeExpr reports whether e is the syntactic Edge[...] instantiation.
114 func (c *edgeAffinityCtx) isEdgeTypeExpr(e syntax.Expr) (ok bool) {
115 ix, isIndex := e.(*syntax.IndexExpr)
116 if !isIndex {
117 return false
118 }
119 n, isName := ix.X.(*syntax.Name)
120 return isName && n.Value == "Edge"
121 }
122 123 func (c *edgeAffinityCtx) isEdgeName(name string) (ok bool) {
124 return c.vars[name]
125 }
126 127 func (c *edgeAffinityCtx) stmts(list []syntax.Stmt) {
128 for i := int32(0); i < int32(len(list)); i++ {
129 c.stmt(list[i])
130 }
131 }
132 133 func (c *edgeAffinityCtx) stmt(st syntax.Stmt) {
134 switch s := st.(type) {
135 case *syntax.BlockStmt:
136 c.stmts(s.List)
137 case *syntax.LabeledStmt:
138 c.stmt(s.Stmt)
139 case *syntax.DeclStmt:
140 for i := int32(0); i < int32(len(s.DeclList)); i++ {
141 vd, ok := s.DeclList[i].(*syntax.VarDecl)
142 if !ok || vd.Values == nil {
143 continue
144 }
145 c.expr(vd.Values, false)
146 }
147 case *syntax.AssignStmt:
148 c.expr(s.Lhs, true)
149 // `_ = e` discards the value: no second owner is created.
150 c.expr(s.Rhs, c.blankTarget(s.Lhs))
151 case *syntax.ExprStmt:
152 c.expr(s.X, false)
153 case *syntax.SendStmt:
154 c.expr(s.Chan, true)
155 c.expr(s.Value, false)
156 case *syntax.ReturnStmt:
157 c.expr(s.Results, true)
158 case *syntax.IfStmt:
159 c.simple(s.Init)
160 c.expr(s.Cond, false)
161 c.stmt(s.Then)
162 if s.Else != nil {
163 c.stmt(s.Else)
164 }
165 case *syntax.ForStmt:
166 c.simple(s.Init)
167 c.expr(s.Cond, false)
168 c.simple(s.Post)
169 c.stmt(s.Body)
170 case *syntax.SwitchStmt:
171 c.simple(s.Init)
172 c.expr(s.Tag, false)
173 for i := int32(0); i < int32(len(s.Body)); i++ {
174 c.expr(s.Body[i].Cases, false)
175 c.stmts(s.Body[i].Body)
176 }
177 case *syntax.SelectStmt:
178 for i := int32(0); i < int32(len(s.Body)); i++ {
179 c.simple(s.Body[i].Comm)
180 c.stmts(s.Body[i].Body)
181 }
182 case *syntax.CallStmt:
183 c.expr(s.Call, false)
184 }
185 }
186 187 // blankTarget reports whether the assignment target is the blank identifier.
188 func (c *edgeAffinityCtx) blankTarget(e syntax.Expr) (ok bool) {
189 n, isName := e.(*syntax.Name)
190 return isName && n.Value == "_"
191 }
192 193 func (c *edgeAffinityCtx) simple(st syntax.SimpleStmt) {
194 if st == nil {
195 return
196 }
197 switch s := st.(type) {
198 case *syntax.AssignStmt:
199 c.expr(s.Lhs, true)
200 c.expr(s.Rhs, false)
201 case *syntax.ExprStmt:
202 c.expr(s.X, false)
203 case *syntax.SendStmt:
204 c.expr(s.Chan, true)
205 c.expr(s.Value, false)
206 case *syntax.RangeClause:
207 c.expr(s.X, false)
208 }
209 }
210 211 // expr walks one expression; allow is true only where reading the value IS the
212 // move or a field read.
213 func (c *edgeAffinityCtx) expr(e syntax.Expr, allow bool) {
214 if e == nil {
215 return
216 }
217 switch x := e.(type) {
218 case *syntax.Name:
219 if !c.isEdgeName(x.Value) || allow {
220 return
221 }
222 c.fail("an Edge cannot be copied; pass it to one spawn or return it")
223 case *syntax.ParenExpr:
224 c.expr(x.X, allow)
225 case *syntax.SelectorExpr:
226 // e.Send / e.Recv: the pair is read, not copied.
227 c.expr(x.X, true)
228 case *syntax.Operation:
229 if x.Y == nil && x.Op == token.And {
230 // &p in the declaration's __edgealloc call.
231 c.expr(x.X, true)
232 return
233 }
234 c.expr(x.X, false)
235 c.expr(x.Y, false)
236 case *syntax.CallExpr:
237 if isEdgeBuiltinCall(x, "spawn") {
238 for i := int32(0); i < int32(len(x.ArgList)); i++ {
239 if i == 0 {
240 c.expr(x.ArgList[i], false)
241 continue
242 }
243 c.expr(x.ArgList[i], true)
244 c.move(x.ArgList[i])
245 }
246 return
247 }
248 if isEdgeBuiltinCall(x, "__edgealloc") {
249 // The declaration: &p, &c and the zero Edge carrying the type.
250 for i := int32(0); i < int32(len(x.ArgList)); i++ {
251 c.expr(x.ArgList[i], true)
252 }
253 return
254 }
255 c.expr(x.Fun, false)
256 for i := int32(0); i < int32(len(x.ArgList)); i++ {
257 c.expr(x.ArgList[i], false)
258 }
259 case *syntax.CompositeLit:
260 261 for i := int32(0); i < int32(len(x.ElemList)); i++ {
262 c.expr(x.ElemList[i], false)
263 }
264 case *syntax.KeyValueExpr:
265 c.expr(x.Key, false)
266 c.expr(x.Value, false)
267 case *syntax.ListExpr:
268 for i := int32(0); i < int32(len(x.ElemList)); i++ {
269 c.expr(x.ElemList[i], allow)
270 }
271 case *syntax.IndexExpr:
272 c.expr(x.X, false)
273 c.expr(x.Index, false)
274 case *syntax.SliceExpr:
275 c.expr(x.X, false)
276 for i := int32(0); i < int32(len(x.Index)); i++ {
277 c.expr(x.Index[i], false)
278 }
279 case *syntax.AssertExpr:
280 c.expr(x.X, false)
281 }
282 }
283 284 // move records the spawn that takes the pair and rejects a second one. Identity
285 // is the name: the pair is a local `var` in this function, and two spawns of one
286 // pair is the fault the doc names.
287 func (c *edgeAffinityCtx) move(e syntax.Expr) {
288 n, ok := e.(*syntax.Name)
289 if !ok || !c.isEdgeName(n.Value) {
290 return
291 }
292 if c.moved[n.Value] {
293 c.fail("this Edge was already passed to spawn; a pair has one child")
294 return
295 }
296 c.moved[n.Value] = true
297 }
298 299 // isEdgeBuiltinCall reports whether the call invokes the named builtin. The
300 // name alone is enough: `spawn` and `__edgealloc` are universe entries.
301 func isEdgeBuiltinCall(c *syntax.CallExpr, name string) (ok bool) {
302 n, isName := c.Fun.(*syntax.Name)
303 return isName && n.Value == name
304 }
305