vile / scheme / syntax_lookup_context.v
283 lines · 268 sloc · 11.04 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4
5// A captured lookup context can resolve identifiers that were absent from the
6// original input. It contains locations and detached lexical frames, never a
7// compiler, scope, function, VM, or any runtime global values. Namespace and
8// transformer metadata still require their publication owner to remain alive.
9@[heap]
10struct SyntaxLookupContext {
11 temporaries &SyntaxTemporaryContext = unsafe { nil }
12 origin &SyntaxLookupContext = unsafe { nil }
13 overlay_frames int
14 overlay_boundary bool
15 overlay_slots int
16 lexical &SyntaxLexicalFrame = unsafe { nil }
17 globals map[string]SyntaxBinding
18 overrides map[string]SyntaxBinding
19 private_bindings &SyntaxPrivateSnapshot = unsafe { nil }
20 global_floor int = -1
21 global_generation u64
22 namespace &Namespace = unsafe { nil }
23 implicit_syntax bool
24 keyword_variables []string
25}
26
27fn (mut b Builder) capture_syntax_lookup_context() !&SyntaxLookupContext {
28 snapshot := b.capture_base_syntax_globals()!
29 private_bindings := if b.cacheable_syntax_globals() {
30 b.capture_private_syntax_bindings()!
31 } else {
32 unsafe { &SyntaxPrivateSnapshot(nil) }
33 }
34 mut overrides := map[string]SyntaxBinding{}
35 if !b.cacheable_syntax_globals() && !isnil(b.code.generated) {
36 reserve_syntax_binding_map(mut overrides, b.code.generated.aliases.len + b.code.generated.globals.len)
37 for name, _ in b.code.generated.aliases {
38 // Exact generated names participate in lookup too; resolve captures
39 // and declaration aliases before detaching compiler-only locations.
40 overrides[name] = own_context_binding(b.freeze_resolved_syntax_binding(name)!, name)!
41 }
42 for name, _ in b.code.generated.globals {
43 // A staged declaration masks an earlier syntax binding/location.
44 overrides[name] = own_context_binding(b.global_syntax_binding(name), name)!
45 }
46 }
47 return &SyntaxLookupContext{
48 lexical: capture_lexical_syntax_context(b.scope)!
49 globals: unsafe { snapshot.bindings }
50 overrides: overrides
51 private_bindings: private_bindings
52 global_floor: b.syntax_global_count()
53 global_generation: b.code.syntax_global_generation_floor()!
54 namespace: b.code.namespace
55 implicit_syntax: isnil(b.code.namespace) || b.code.namespace.implicit_syntax
56 keyword_variables: b.code.keyword_variables.map(it.clone())
57 }
58}
59
60// Map iteration already owns the lookup key. Canonical imported names can
61// differ from that key, so retain an owned spelling for those as well.
62fn own_context_binding(input SyntaxBinding, key string) !SyntaxBinding {
63 binding := freeze_syntax_binding(input)!
64 return SyntaxBinding{
65 ...binding
66 name: if binding.name == key { key } else { binding.name.clone() }
67 introduced: if binding.introduced.len > 0 { binding.introduced.clone() } else { '' }
68 introduced_key: if binding.introduced_key.len > 0 {
69 binding.introduced_key.clone()
70 } else {
71 ''
72 }
73 }
74}
75
76fn (context &SyntaxLookupContext) binding(name string) SyntaxBinding {
77 if name in [implicit_statement_name, 'vile:statement'] {
78 return SyntaxBinding{ kind: .keyword, name: implicit_statement_name }
79 }
80 if binding := lexical_syntax_context_binding(context.lexical, name) { return binding }
81 if binding := context.overrides[name] { return binding }
82 if !isnil(context.private_bindings) {
83 if binding := context.private_bindings.binding(name) { return binding }
84 }
85 if binding := context.globals[name] { return binding }
86 return if context.implicit_syntax && special_name(name) && name !in context.keyword_variables {
87 SyntaxBinding{ kind: .keyword, name: name, namespace: context.namespace }
88 } else {
89 SyntaxBinding{ kind: .global, name: name, namespace: context.namespace, unbound: true }
90 }
91}
92
93struct SyntaxLookupContextRef {
94 value &SyntaxLookupContext
95}
96
97// An origin overlay adds only substitutions born after its anchor was emitted.
98// Older caller bindings must never replace the anchor's definition-site context.
99fn (context &SyntaxLookupContext) binding_with_marks(name string, history &SyntaxWrap) SyntaxBinding {
100 wrap := if !isnil(history) && history.antimark { history.parent } else { history }
101 if isnil(wrap) && isnil(context.origin) { return context.binding(name) }
102 if isnil(context.origin) && !isnil(context.temporaries) {
103 if binding := context.temporaries.binding(name, wrap) { return binding }
104 }
105 storage := arena.new(chunk_size: 1024)
106 defer { release_frozen_syntax_scratch(storage) }
107 key := syntax_marked_lookup_key(name, wrap, storage)
108 mut cursor := SyntaxLookupContextRef{context}
109 for !isnil(cursor.value) {
110 current := cursor.value
111 mut frames := SyntaxLexicalFrameRef{current.lexical}
112 if isnil(current.origin) {
113 if !isnil(current.temporaries) {
114 if binding := current.temporaries.binding(name, wrap) { return binding }
115 }
116 for !isnil(frames.value) {
117 if binding := frames.value.marked_binding(key, name, -1) { return binding }
118 frames = SyntaxLexicalFrameRef{frames.value.parent}
119 }
120 return if isnil(wrap) {
121 current.binding(name)
122 } else {
123 current.marked_global_binding(name, key)
124 }
125 }
126 for _ in 0 .. current.overlay_frames {
127 if isnil(frames.value) { break }
128 if binding := frames.value.marked_binding(key, name, -1) { return binding }
129 if isnil(wrap) {
130 if binding := frames.value.marked_binding(name, name, -1) { return binding }
131 }
132 frames = SyntaxLexicalFrameRef{frames.value.parent}
133 }
134 if current.overlay_boundary && !isnil(frames.value) {
135 if binding := frames.value.marked_binding(key, name, current.overlay_slots) {
136 return binding
137 }
138 if isnil(wrap) {
139 if binding := frames.value.marked_binding(name, name, current.overlay_slots) {
140 return binding
141 }
142 }
143 }
144 cursor = SyntaxLookupContextRef{current.origin}
145 }
146 return context.binding(name)
147}
148
149fn syntax_marked_lookup_key(name string, wrap &SyntaxWrap, storage arena.Arena) string {
150 family := syntax_capture_spelling(wrap, storage, true)
151 storage.push()
152 defer { storage.pop() }
153 return '${family}/${external_identifier_spelling(name)}'
154}
155
156// A datum can introduce any spelling with the anchor's context, including one
157// absent from the original template. Resolve the marked global spelling before
158// the public spelling, and carry a declaration fence if it is still pending.
159// The scratch lookup key must never escape in the returned binding.
160fn (context &SyntaxLookupContext) marked_global_binding(name string, key string) SyntaxBinding {
161 if binding := context.overrides[key] { return binding }
162 if !isnil(context.private_bindings) {
163 if binding := context.private_bindings.binding(key) { return binding }
164 }
165 if binding := context.globals[key] { return binding }
166 binding := context.binding(name)
167 if !isnil(context.temporaries) || binding.kind == .lexical
168 || (binding.kind == .transformer && !binding.top_level) {
169 return binding
170 }
171 return SyntaxBinding{
172 ...binding
173 introduced: key.clone()
174 introduced_key: ''
175 introduced_namespace: context.namespace
176 introduced_globals: context.global_floor
177 introduced_generation: context.global_generation
178 }
179}
180
181fn (frame &SyntaxLexicalFrame) marked_binding(key string, owned_name string, first_slot int) ?SyntaxBinding {
182 if slot := frame.names[key] {
183 if slot >= first_slot {
184 // The lookup key can live in scratch storage; the binding retains only the
185 // caller-owned spelling and detached lexical location.
186 return SyntaxBinding{ kind: .lexical, name: owned_name, slot: slot, lexical_domain: frame.domain, lexical_id: frame.function_id }
187 }
188 }
189 if binding := frame.syntax[key] {
190 if first_slot < 0 { return binding }
191 if binding.kind == .transformer && !isnil(binding.transformer) && !isnil(binding.transformer.enumeration) {
192 hidden := binding.transformer.enumeration.binding
193 if hidden.kind == .lexical && hidden.slot >= first_slot && hidden.lexical_id == frame.function_id && voidptr(hidden.lexical_domain) == voidptr(frame.domain) {
194 return binding
195 }
196 }
197 }
198 return none
199}
200
201// Only the immutable map is published. Cache identity and invalidation fields
202// remain with the compiler and never enter a syntax object's ownership graph.
203struct SyntaxGlobalSnapshot {
204 bindings map[string]SyntaxBinding
205}
206
207struct SyntaxGlobalCache {
208 owner &Owner
209 namespace &Namespace = unsafe { nil }
210 syntax_table &SyntaxBindings = unsafe { nil }
211 epoch u64
212 writable bool
213 snapshot &SyntaxGlobalSnapshot
214}
215
216fn (mut b Builder) capture_base_syntax_globals() !&SyntaxGlobalSnapshot {
217 cacheable := b.cacheable_syntax_globals()
218 if cacheable && isnil(b.code.syntax_base) {
219 vm := unsafe { &VM(b.code.compiler_vm) }
220 b.code.syntax_base = if isnil(b.code.namespace) {
221 vm.syntax
222 } else {
223 b.code.namespace.syntax
224 }
225 if !isnil(b.code.syntax_base) && b.code.syntax_base.entries.len > 0 {
226 b.code.has_syntax = true
227 }
228 }
229 epoch := if cacheable { unsafe { (&VM(b.code.compiler_vm)).binding_epoch } } else { u64(0) }
230 writable := isnil(b.code.namespace) || b.code.namespace.writable
231 if cacheable && !isnil(b.code.procedural) && !isnil(b.code.procedural.globals_cache) {
232 prior := b.code.procedural.globals_cache
233 if prior.epoch == epoch && voidptr(prior.owner) == voidptr(b.syntax_owner()) && voidptr(prior.namespace) == voidptr(b.code.namespace) && voidptr(prior.syntax_table) == voidptr(b.code.syntax_base) && prior.writable == writable {
234 return prior.snapshot
235 }
236 }
237 mut globals := map[string]SyntaxBinding{}
238 estimated := (if isnil(b.code.known_globals) { 0 } else { b.code.known_globals.len }) + (if isnil(b.code.syntax_base) {
239 0
240 } else {
241 b.code.syntax_base.entries.len
242 })
243 reserve_syntax_binding_map(mut globals, estimated)
244 if !isnil(b.code.known_globals) {
245 for name, _ in *b.code.known_globals {
246 globals[name] = own_context_binding(b.base_global_syntax_binding(name), name)!
247 }
248 }
249 if !isnil(b.code.syntax_base) {
250 for name, _ in b.code.syntax_base.entries {
251 if name !in globals {
252 globals[name] = own_context_binding(b.base_global_syntax_binding(name), name)!
253 }
254 }
255 }
256 snapshot := &SyntaxGlobalSnapshot{ bindings: globals }
257 if cacheable {
258 mut metadata := b.code.procedural_syntax()
259 metadata.globals_cache = &SyntaxGlobalCache{
260 owner: b.syntax_owner()
261 namespace: b.code.namespace
262 syntax_table: b.code.syntax_base
263 epoch: epoch
264 writable: writable
265 snapshot: snapshot
266 }
267 }
268 return snapshot
269}
270
271fn (b &Builder) cacheable_syntax_globals() bool {
272 if isnil(b.code.compiler_vm) || isnil(b.code.known_globals) { return false }
273 vm := unsafe { &VM(b.code.compiler_vm) }
274 expected := if isnil(b.code.namespace) { vm.bindings } else { &b.code.namespace.bindings }
275 live_syntax := if isnil(b.code.namespace) { vm.syntax } else { b.code.namespace.syntax }
276 return voidptr(b.code.known_globals) == voidptr(expected) && (isnil(b.code.syntax_base) || voidptr(b.code.syntax_base) == voidptr(live_syntax))
277}
278
279// Arena storage retains old map growth slabs. The input tables provide a cheap
280// upper bound; use the stdlib reservation rather than accumulating those copies.
281fn reserve_syntax_binding_map(mut bindings map[string]SyntaxBinding, capacity int) {
282 if capacity > 0 && u64(capacity) <= u64(~u32(0)) { bindings.reserve(u32(capacity)) }
283}
284