Repository navigation
Expand file tree
/
Copy pathcstruct_types.go
More file actions
286 lines (278 loc) · 9.08 KB
/
Copy pathcstruct_types.go
File metadata and controls
286 lines (278 loc) · 9.08 KB
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
package main
// CStructTypes is the platform-independent cstruct type oracle.
//
// Inferring which cstruct type an expression produces — through casts,
// constructors, function returns, if/match arms, nested fields, list elements,
// and inlined blocks — is pure semantic analysis with no architecture in it.
// Every code generator needs the exact same answers, so the logic lives here
// once and each backend delegates to it. Previously each backend carried its
// own copy and they drifted (the ARM64 copy learned func().field, ternary/if
// results and nested fields while the x86 copy still only knew casts and plain
// identifiers), which is exactly the kind of per-platform divergence this type
// exists to prevent.
//
// The maps are held by reference: a backend constructs CStructTypes over the
// same map instances it fills during compilation, so registrations it performs
// (and the temporary ones RegisterBlockLocals adds) are visible here and vice
// versa. The oracle never reassigns a map, only reads and mutates in place.
type CStructTypes struct {
Decls map[string]*CStructDecl // cstruct name -> declaration (field layout)
VarType map[string]string // variable name -> cstruct type it points at
FuncReturns map[string]string // function/lambda name -> cstruct type its body returns
VarListElem map[string]string // list-variable name -> cstruct element type
FuncReturnsListElem map[string]string // function name -> cstruct element type of the list it returns
}
// TypeOf returns the name of the cstruct type expr evaluates to, or "" if expr
// is not a (known) cstruct value. Recognizes `as Struct` casts, struct
// constructors `Struct(...)`, calls to functions known to return a struct,
// identifiers already tracked as struct-typed, if/match results, nested cstruct
// fields, and blocks (via their final expression).
func (c *CStructTypes) TypeOf(expr Expression) string {
switch e := expr.(type) {
case *CastExpr:
if _, ok := c.Decls[e.Type]; ok {
return e.Type
}
case *CallExpr:
if _, ok := c.Decls[e.Function]; ok {
return e.Function
}
if t, ok := c.FuncReturns[e.Function]; ok {
return t
}
case *IdentExpr:
if t, ok := c.VarType[e.Name]; ok {
return t
}
case *BlockExpr:
// A block (e.g. an inlined function body) evaluates to its last
// expression — look through it so `r = { …; V(…) }` is typed as V.
if n := len(e.Statements); n > 0 {
switch s := e.Statements[n-1].(type) {
case *ExpressionStmt:
return c.TypeOf(s.Expr)
case *JumpStmt:
if s.Value != nil {
return c.TypeOf(s.Value)
}
case *IfStmt:
return c.ifStmtType(s)
}
}
case *MatchExpr:
// `if c { a } else { b }` / guard match: every arm yields the same type,
// so the result is that of any arm that evaluates to a cstruct.
for _, cl := range e.Clauses {
if cl.Result != nil {
if t := c.TypeOf(cl.Result); t != "" {
return t
}
}
}
if e.DefaultExpr != nil {
return c.TypeOf(e.DefaultExpr)
}
case *FieldAccessExpr:
// A nested cstruct-valued field, e.g. `b.c` where Ball has `c: V`.
if decl := c.DeclForExpr(e); decl != nil {
return decl.Name
}
}
return ""
}
// DeclForExpr returns the cstruct declaration expr evaluates to (for field
// offset resolution), or nil. Handles identifiers, casts, list-element indexing
// (`xs[i]`), nested cstruct fields used as receivers (`b.c.x`), and call results
// used directly as receivers (`mk(...).x`).
func (c *CStructTypes) DeclForExpr(expr Expression) *CStructDecl {
switch e := expr.(type) {
case *IdentExpr:
if name, ok := c.VarType[e.Name]; ok {
return c.Decls[name]
}
case *CastExpr:
if decl, ok := c.Decls[e.Type]; ok {
return decl
}
case *IndexExpr:
// `xs[i]` where xs is a known list of cstructs: the element is a pointer
// to that cstruct, so `xs[i].field` reads at the field offset.
if t := c.listElemTypeOf(e.List); t != "" {
return c.Decls[t]
}
case *FieldAccessExpr:
// A nested cstruct-valued field used as a receiver: `b.c.x` where `b.c`
// is itself a cstruct (V). Resolve the outer object's struct, find the
// field, and return the field's own struct declaration.
if decl := c.DeclForExpr(e.Object); decl != nil {
for i := range decl.Fields {
if decl.Fields[i].Name == e.FieldName && decl.Fields[i].StructName != "" {
return c.Decls[decl.Fields[i].StructName]
}
}
}
case *CallExpr:
// A call result used directly as a receiver: `mk(...).x`. A constructor
// yields its own struct; any other call yields the struct its function
// is known to return. This is what lets `f(...).field` work without
// first binding the result to a typed local.
if decl, ok := c.Decls[e.Function]; ok {
return decl
}
if t, ok := c.FuncReturns[e.Function]; ok {
return c.Decls[t]
}
}
return nil
}
// ifStmtType returns the cstruct type a trailing `if`/`elif`/`else` statement
// evaluates to (the type of any arm's final value). A struct-returning function
// may end in such a statement (e.g. a normalize-or-self), and without this its
// callers wouldn't know the result is a cstruct pointer.
func (c *CStructTypes) ifStmtType(s *IfStmt) string {
blockType := func(body []Statement) string {
if n := len(body); n > 0 {
switch st := body[n-1].(type) {
case *ExpressionStmt:
return c.TypeOf(st.Expr)
case *JumpStmt:
if st.Value != nil {
return c.TypeOf(st.Value)
}
case *IfStmt:
return c.ifStmtType(st)
}
}
return ""
}
for _, br := range s.Branches {
if t := blockType(br.Body); t != "" {
return t
}
}
return blockType(s.ElseBody)
}
// listElemTypeOf infers the cstruct type of the ELEMENTS of a list-valued
// expression, or "". This lets `xs[i].field` resolve the field offset when xs
// is a list of cstructs (`[Ball(...), Ball(...)]`, a list-returning call, or a
// variable holding one).
func (c *CStructTypes) listElemTypeOf(expr Expression) string {
switch e := expr.(type) {
case *ListExpr:
if len(e.Elements) > 0 {
return c.TypeOf(e.Elements[0])
}
case *IdentExpr:
if t, ok := c.VarListElem[e.Name]; ok {
return t
}
case *CallExpr:
if t, ok := c.FuncReturnsListElem[e.Function]; ok {
return t
}
case *BlockExpr:
if n := len(e.Statements); n > 0 {
switch s := e.Statements[n-1].(type) {
case *ExpressionStmt:
return c.listElemTypeOf(s.Expr)
case *JumpStmt:
if s.Value != nil {
return c.listElemTypeOf(s.Value)
}
}
}
}
return ""
}
// LambdaReturnsListElem returns the cstruct element type of the list a lambda
// body evaluates to, or "" — so `f = () -> [Ball(...), ...]` lets `bs = f()`
// know `bs[i]` is a Ball.
func (c *CStructTypes) LambdaReturnsListElem(body Expression) string {
last := body
if b, ok := body.(*BlockExpr); ok {
if len(b.Statements) == 0 {
return ""
}
switch s := b.Statements[len(b.Statements)-1].(type) {
case *ExpressionStmt:
last = s.Expr
case *JumpStmt:
last = s.Value
default:
return ""
}
}
if last == nil {
return ""
}
return c.listElemTypeOf(last)
}
// LambdaReturnType returns the cstruct type a lambda body evaluates to (its last
// expression), or "" — used so `vadd = (a,b)->{...Point(...)}` lets
// `p = vadd(a,b)` know p is a Point.
func (c *CStructTypes) LambdaReturnType(body Expression) string {
last := body
if b, ok := body.(*BlockExpr); ok {
if len(b.Statements) == 0 {
return ""
}
// Pre-register the body's local cstruct types so a returned local
// identifier (`r = …; r`) resolves even though the body has not been
// compiled yet. Restore afterward so these locals don't leak globally.
added := c.RegisterBlockLocals(b.Statements)
defer func() {
for _, k := range added {
delete(c.VarType, k)
}
}()
switch s := b.Statements[len(b.Statements)-1].(type) {
case *ExpressionStmt:
last = s.Expr
case *JumpStmt:
last = s.Value
case *IfStmt:
// A function may end in a bare `if`/`else` whose arms yield a cstruct.
return c.ifStmtType(s)
default:
return ""
}
}
if last == nil {
return ""
}
return c.TypeOf(last)
}
// RegisterBlockLocals scans a block's statements in order and records the
// cstruct type of each struct-valued local into VarType (so later statements —
// and a trailing `return local` — can resolve it). It only adds names not
// already tracked, and returns the list of names it added so the caller can
// restore the map. It descends into `arena { … }` and `with … { … }` blocks,
// which wrap most struct-temporary scopes, but not control flow (a
// conditionally-defined local has no single static type).
func (c *CStructTypes) RegisterBlockLocals(stmts []Statement) []string {
var added []string
var scan func(ss []Statement)
scan = func(ss []Statement) {
for _, stmt := range ss {
switch s := stmt.(type) {
case *AssignStmt:
if s.IsUpdate {
continue
}
if _, exists := c.VarType[s.Name]; exists {
continue
}
if ct := c.TypeOf(s.Value); ct != "" {
c.VarType[s.Name] = ct
added = append(added, s.Name)
}
case *ArenaStmt:
scan(s.Body)
case *WithStmt:
scan(s.Body)
}
}
}
scan(stmts)
return added
}