vile / scheme / syntax_identifier.v
401 lines · 379 sloc · 15.18 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3enum SyntaxBindingKind as u8 {
4 global
5 lexical
6 keyword
7 transformer
8}
9
10// Lexical references name a function's binding location, never its current
11// value. This preserves assignment and works through extra compile-time regions
12// that share a single runtime activation.
13struct SyntaxBinding {
14 pattern_variable &SyntaxCaseVariable = unsafe { nil }
15 kind SyntaxBindingKind
16 lexical_id u32
17 name string
18 function &Function = unsafe { nil }
19 slot int
20 transformer &SyntaxTransformer = unsafe { nil }
21 top_level bool
22 introduced string
23 introduced_key string
24 introduced_globals int = -1
25 introduced_generation u64
26 namespace &Namespace = unsafe { nil }
27 introduced_namespace &Namespace = unsafe { nil }
28 global_slot int = -1
29 assignable bool
30 unbound bool
31 lexical_domain &SyntaxLexicalDomain = unsafe { nil }
32}
33
34struct IdentifierAlias {
35 context &SyntaxLookupContext = unsafe { nil }
36 capture_key string
37 capture_scope &Scope = unsafe { nil }
38 capture_slots int
39 capture_globals int = -1
40 capture_generation u64
41 binding SyntaxBinding
42 use_site bool
43 use_scope &Scope = unsafe { nil }
44 use_name string
45 wrap &SyntaxWrap = unsafe { nil }
46}
47
48fn same_syntax_binding(a SyntaxBinding, b SyntaxBinding) bool {
49 if a.kind != b.kind { return false }
50 if a.kind == .lexical {
51 if isnil(a.pattern_variable) != isnil(b.pattern_variable) { return false }
52 if !isnil(a.pattern_variable) && a.pattern_variable.variable != b.pattern_variable.variable {
53 return false
54 }
55 return a.slot == b.slot && same_lexical_syntax_function(a, b)
56 }
57 if a.kind == .transformer {
58 return if a.top_level && b.top_level {
59 a.name == b.name && voidptr(a.namespace) == voidptr(b.namespace)
60 } else {
61 voidptr(a.transformer) == voidptr(b.transformer)
62 }
63 }
64 if a.kind == .global {
65 if a.unbound || b.unbound { return a.unbound && b.unbound && a.name == b.name }
66 if same := same_report_binding(a, b) { return same }
67 if a.global_slot >= 0 && b.global_slot >= 0 { return a.global_slot == b.global_slot }
68 return a.name == b.name && voidptr(a.namespace) == voidptr(b.namespace)
69 }
70 return a.name == b.name
71}
72
73// Report environments are one immutable set of locations per VM, even when a
74// shorter caller scope needs an uncached namespace wrapper. Explicit imports
75// may have materialized the same original location as an absolute VM slot.
76fn same_report_binding(a SyntaxBinding, b SyntaxBinding) ?bool {
77 a_report := report_slot(a.namespace, a.global_slot)
78 b_report := report_slot(b.namespace, b.global_slot)
79 if !a_report && !b_report { return none }
80 if a_report && b_report {
81 return voidptr(a.namespace.report_owner) == voidptr(b.namespace.report_owner) && a.global_slot == b.global_slot
82 }
83 if a_report {
84 if slot := a.namespace.report_owner.report_slots[-a.global_slot - 2] {
85 return slot == b.global_slot
86 }
87 } else {
88 if slot := b.namespace.report_owner.report_slots[-b.global_slot - 2] {
89 return slot == a.global_slot
90 }
91 }
92 return false
93}
94
95fn scope_binding(scope &Scope, name string) ?SyntaxBinding {
96 mut cursor := ScopeRef{scope}
97 for !isnil(cursor.value) {
98 if slot := cursor.value.names[name] {
99 return SyntaxBinding{ kind: .lexical, name: name, function: cursor.value.function, slot: slot }
100 }
101 if !isnil(cursor.value.syntax) {
102 if binding := cursor.value.syntax.entries[name] { return binding }
103 }
104 cursor = ScopeRef{cursor.value.parent}
105 }
106 return none
107}
108
109fn (b &Builder) resolve_binding(name string) SyntaxBinding {
110 // Reader lowering uses one reserved compiler form. Its printed spelling
111 // must retain the same identity through lexical variables and transformers.
112 if name in [implicit_statement_name, 'vile:statement'] {
113 return SyntaxBinding{ kind: .keyword, name: implicit_statement_name }
114 }
115 if binding := scope_binding(b.scope, name) { return binding }
116 if !isnil(b.code.generated) {
117 if binding := b.code.generated.globals[name] { return binding }
118 if alias := b.code.generated.aliases[name] {
119 // A staged sibling can inherit an alias before the declaration's compiler
120 // canonicalizes it. Resolve the same marked binder key in either child.
121 capture_key := if alias.capture_key != '' {
122 alias.capture_key
123 } else {
124 b.code.binding_key(name)
125 }
126 if capture_key != '' {
127 if binding := scope_capture_binding(b.scope, capture_key, alias.capture_scope, alias.capture_slots) {
128 return binding
129 }
130 if binding := b.code.generated.globals[capture_key] { return binding }
131 if b.known_global(capture_key) {
132 candidate := b.global_syntax_binding(capture_key)
133 if private_syntax_location_allowed(b.syntax_owner(), SyntaxBinding{
134 ...alias.binding
135 introduced: capture_key
136 introduced_namespace: b.code.namespace
137 introduced_globals: alias.capture_globals
138 introduced_generation: alias.capture_generation
139 }, runtime_syntax_frozen(candidate)) {
140 return candidate
141 }
142 }
143 }
144 // Exact private aliases installed by a declaration retain its location
145 // across staged namespace compilation, including captured identifiers.
146 if b.known_global(name) { return b.global_syntax_binding(name) }
147 if alias.use_site {
148 if binding := scope_binding(alias.use_scope, alias.use_name) { return binding }
149 if binding := b.code.generated.globals[alias.use_name] { return binding }
150 }
151 binding := b.current_syntax_binding(alias.binding)
152 return if !alias.use_site && binding.kind == .global && binding.introduced == '' {
153 SyntaxBinding{
154 ...binding
155 introduced: name
156 introduced_key: if capture_key != name { capture_key } else { '' }
157 introduced_namespace: b.code.namespace
158 introduced_globals: alias.capture_globals
159 introduced_generation: alias.capture_generation
160 }
161 } else {
162 binding
163 }
164 }
165 }
166 return b.global_syntax_binding(name)
167}
168
169fn (b &Builder) current_syntax_binding(binding SyntaxBinding) SyntaxBinding {
170 // Selected record auxiliary exports use canonical syntax identities rather
171 // than executable namespace spellings. Preserve them through exported macros.
172 if binding.kind == .keyword && (binding.name.starts_with('record-aux:') || binding.name in [
173 implicit_statement_name,
174 'vile:statement',
175 ]) {
176 return binding
177 }
178 if binding.introduced != '' && b.known_global_in(binding.introduced_namespace, binding.introduced) {
179 candidate := b.global_syntax_binding_in(binding.introduced_namespace, binding.introduced)
180 if private_syntax_location_allowed(b.syntax_owner(), binding, runtime_syntax_frozen(candidate)) {
181 return candidate
182 }
183 }
184 if binding.introduced_key != '' && binding.introduced_key != binding.introduced && b.known_global_in(binding.introduced_namespace, binding.introduced_key) {
185 candidate := b.global_syntax_binding_in(binding.introduced_namespace, binding.introduced_key)
186 if private_syntax_named_location_allowed(b.syntax_owner(), binding, binding.introduced_key, runtime_syntax_frozen(candidate)) {
187 return candidate
188 }
189 }
190 if binding.kind == .lexical || (binding.kind == .transformer && !binding.top_level) {
191 return binding
192 }
193 // Existing variable references identify locations. Imports can rebind the
194 // same spelling without changing a frozen macro's definition-site binding.
195 if binding.kind == .global && binding.global_slot >= 0 { return binding }
196 resolved := b.global_syntax_binding_in(binding.namespace, binding.name)
197 return if binding.introduced != '' && resolved.kind == .global {
198 SyntaxBinding{
199 ...resolved
200 introduced: binding.introduced
201 introduced_key: binding.introduced_key
202 introduced_namespace: binding.introduced_namespace
203 introduced_globals: binding.introduced_globals
204 introduced_generation: binding.introduced_generation
205 }
206 } else {
207 resolved
208 }
209}
210
211fn (b &Builder) known_global(name string) bool {
212 return b.known_global_in(b.code.namespace, name)
213}
214
215fn (b &Builder) known_global_in(space &Namespace, name string) bool {
216 if voidptr(space) != voidptr(b.code.namespace) {
217 return !isnil(b.code.compiler_vm) && unsafe { (&VM(b.code.compiler_vm)).namespace_has_binding(space, name) }
218 }
219 if !isnil(b.code.generated) && name in b.code.generated.globals { return true }
220 syntax_table := b.current_syntax_table()
221 if !isnil(syntax_table) && name in syntax_table.entries { return true }
222 return !isnil(b.code.known_globals) && name in b.code.known_globals
223}
224
225fn (b &Builder) global_syntax_binding(name string) SyntaxBinding {
226 return b.global_syntax_binding_in(b.code.namespace, name)
227}
228
229fn (b &Builder) global_syntax_binding_in(space &Namespace, name string) SyntaxBinding {
230 if voidptr(space) != voidptr(b.code.namespace) {
231 if !isnil(b.code.compiler_vm) {
232 return unsafe { (&VM(b.code.compiler_vm)).namespace_binding(space, name) }
233 }
234 return SyntaxBinding{ kind: .global, name: name, namespace: space, unbound: true }
235 }
236 if !isnil(b.code.generated) {
237 if binding := b.code.generated.globals[name] { return binding }
238 }
239
240 return b.base_global_syntax_binding(name)
241}
242
243// The immutable base excludes this compiler's staged declaration overlay.
244fn (b &Builder) base_global_syntax_binding(name string) SyntaxBinding {
245 space := b.code.namespace
246 syntax_table := b.current_syntax_table()
247 if !isnil(syntax_table) {
248 if binding := syntax_table.entries[name] {
249 return if !isnil(b.code.compiler_vm) {
250 unsafe { (&VM(b.code.compiler_vm)).imported_syntax_binding(space, name, binding) }
251 } else {
252 binding
253 }
254 }
255 }
256 if !isnil(b.code.known_globals) {
257 if slot := (*b.code.known_globals)[name] {
258 assignable := if !isnil(b.code.compiler_vm) {
259 unsafe { (&VM(b.code.compiler_vm)).assignment_permitted_in(space, name) }
260 } else {
261 isnil(space) || space.writable
262 }
263 return SyntaxBinding{ kind: .global, name: name, namespace: space, global_slot: slot, assignable: assignable }
264 }
265 }
266 return if name in [implicit_statement_name, 'vile:statement'] || ((isnil(space) || space.implicit_syntax) && special_name(name) && name !in b.code.keyword_variables) {
267 SyntaxBinding{ kind: .keyword, name: name, namespace: space }
268 } else {
269 SyntaxBinding{ kind: .global, name: name, namespace: space, unbound: true }
270 }
271}
272
273fn (b &Builder) current_syntax_table() &SyntaxBindings {
274 if !isnil(b.code.syntax_base) { return b.code.syntax_base }
275 // A native reentry can install the first syntax table after this Code was
276 // created. Keep prepared/offline tables authoritative; only official live
277 // namespace borrows observe that newly allocated table.
278 if !b.cacheable_syntax_globals() { return unsafe { nil } }
279 vm := unsafe { &VM(b.code.compiler_vm) }
280 return if isnil(b.code.namespace) { vm.syntax } else { b.code.namespace.syntax }
281}
282
283fn (b &Builder) binding_depth(function &Function) !int {
284 mut cursor := ScopeRef{b.scope}
285 mut depth := 0
286 for !isnil(cursor.value) {
287 if voidptr(cursor.value.function) == voidptr(function) { return depth }
288 parent := cursor.value.parent
289 if !isnil(parent) && voidptr(parent.function) != voidptr(cursor.value.function) { depth++ }
290 cursor = ScopeRef{parent}
291 }
292 return failure('lexical identifier is outside its binding region')
293}
294
295fn (mut code Code) identifier_alias(alias IdentifierAlias) !string {
296 mut arena := code.syntax_arena()
297 if arena.compiled { return failure('cannot change compiled syntax') }
298 arena.serial++
299 name := '\x00${code.syntax_seed}.${arena.serial}'
300 installed := if alias.capture_globals >= 0 && alias.capture_generation == 0 {
301 IdentifierAlias{ ...alias, capture_generation: code.syntax_global_generation_floor()! }
302 } else {
303 alias
304 }
305 arena.set_alias(name, installed)
306 return name
307}
308
309fn (mut b Builder) declare_definition(name string) int {
310 if !isnil(b.code.generated) {
311 if alias := b.code.generated.aliases[name] {
312 if (alias.use_site && voidptr(alias.use_scope) == voidptr(b.scope)) || (alias.capture_key != '' && isnil(alias.wrap) && alias.use_name != '') {
313 slot := b.scope.declare(alias.use_name)
314 b.scope.names[name] = slot
315 if alias.capture_key != '' { b.scope.names[alias.capture_key] = slot }
316 return slot
317 }
318 }
319 }
320 return b.scope.declare(name)
321}
322
323fn (mut b Builder) global_definition_name(name string) string {
324 mut target := name
325 if isnil(b.code.generated) && !isnil(b.code.syntax_base) && name in b.code.syntax_base.entries {
326 // A variable definition must mask its previous transformer while its
327 // initializer compiles, before runtime updates the borrowed VM table.
328 b.code.syntax_arena()
329 }
330 if !isnil(b.code.generated) {
331 if alias := b.code.generated.aliases[name] {
332 if (alias.use_site && isnil(alias.use_scope)) || (alias.capture_key != '' && isnil(alias.wrap) && alias.use_name != '') {
333 target = alias.use_name
334 }
335 if !alias.use_site && alias.capture_key != '' && !isnil(alias.wrap) {
336 target = alias.capture_key
337 }
338 }
339 binding := SyntaxBinding{ kind: .global, name: target, namespace: b.code.namespace }
340 b.code.generated.set_global_binding(name, binding)
341 b.code.generated.set_global_binding(target, binding)
342 }
343 if special_name(target) && target !in b.code.keyword_variables {
344 b.code.keyword_variables << target
345 }
346 return target
347}
348
349fn scope_capture_binding(scope &Scope, name string, boundary &Scope, slots int) ?SyntaxBinding {
350 mut cursor := ScopeRef{scope}
351 for !isnil(cursor.value) {
352 current := cursor.value
353 at_boundary := voidptr(current) == voidptr(boundary)
354 if slot := current.names[name] {
355 if !at_boundary || slot >= slots {
356 return SyntaxBinding{ kind: .lexical, name: name, function: current.function, slot: slot }
357 }
358 }
359 if at_boundary {
360 if !isnil(current.syntax) {
361 if binding := current.syntax.entries[name] {
362 if binding.kind == .transformer && !isnil(binding.transformer.enumeration) {
363 hidden := binding.transformer.enumeration.binding
364 if hidden.kind == .lexical && hidden.slot >= slots && syntax_binding_function_matches(hidden, current.function) {
365 return binding
366 }
367 }
368 }
369 }
370 return none
371 }
372 if !isnil(current.syntax) {
373 if binding := current.syntax.entries[name] { return binding }
374 }
375 cursor = ScopeRef{current.parent}
376 }
377 return none
378}
379
380// Semantic binding equality deliberately ignores alias spelling and namespace
381// permissions. Transporting one occurrence must preserve those fields too.
382fn same_syntax_transport(a SyntaxBinding, b SyntaxBinding) bool {
383 return same_syntax_binding(a, b) && a.name == b.name && a.slot == b.slot
384 && a.global_slot == b.global_slot && a.assignable == b.assignable
385 && a.unbound == b.unbound && a.top_level == b.top_level
386 && a.introduced == b.introduced && voidptr(a.namespace) == voidptr(b.namespace)
387 && a.introduced_globals == b.introduced_globals && a.introduced_generation == b.introduced_generation
388 && a.introduced_key == b.introduced_key
389 && voidptr(a.introduced_namespace) == voidptr(b.introduced_namespace)
390 && voidptr(a.function) == voidptr(b.function) && a.lexical_id == b.lexical_id
391 && voidptr(a.lexical_domain) == voidptr(b.lexical_domain)
392 && voidptr(a.transformer) == voidptr(b.transformer)
393 && voidptr(a.pattern_variable) == voidptr(b.pattern_variable)
394}
395
396fn (b &Builder) identifier_context(name string) &SyntaxLookupContext {
397 if !isnil(b.code.generated) {
398 if alias := b.code.generated.aliases[name] { return alias.context }
399 }
400 return unsafe { nil }
401}
402