| 1 | module scheme |
| 2 | |
| 3 | // Runtime comparison needs declaration locations, never their values. These |
| 4 | // cells are installed only by actual definitions/imports, under the same owner |
| 5 | // contract as the VM's existing binding maps. Capturing or comparing syntax |
| 6 | // never mutates this table or publishes compiler/VM borrows. |
| 7 | @[heap] |
| 8 | struct SyntaxGlobalLocation { |
| 9 | mut: |
| 10 | kind SyntaxBindingKind |
| 11 | name string |
| 12 | namespace u64 |
| 13 | slot int = -1 |
| 14 | generation u64 |
| 15 | origin &SyntaxGlobalLocation = unsafe { nil } |
| 16 | } |
| 17 | |
| 18 | struct SyntaxGlobalLocationRef { |
| 19 | value &SyntaxGlobalLocation |
| 20 | } |
| 21 | |
| 22 | fn (location &SyntaxGlobalLocation) canonical() &SyntaxGlobalLocation { |
| 23 | mut cursor := SyntaxGlobalLocationRef{location} |
| 24 | for !isnil(cursor.value.origin) { cursor = SyntaxGlobalLocationRef{cursor.value.origin} } |
| 25 | return cursor.value |
| 26 | } |
| 27 | |
| 28 | fn syntax_global_location(owner &Owner, space &Namespace, name string) ?&SyntaxGlobalLocation { |
| 29 | if isnil(owner) { return none } |
| 30 | if isnil(space) { |
| 31 | return owner.syntax_locations[name] or { return none } |
| 32 | } |
| 33 | return space.syntax_locations[name] or { return none } |
| 34 | } |
| 35 | |
| 36 | struct PreparedSyntaxLocation { |
| 37 | location &SyntaxGlobalLocation = unsafe { nil } |
| 38 | name string |
| 39 | created bool |
| 40 | } |
| 41 | |
| 42 | // Allocate the name and table entry before changing a visible binding. No |
| 43 | // Scheme code runs between preparing the location and committing or undoing it. |
| 44 | fn (mut vm VM) prepare_syntax_location(space &Namespace, key string, name string) PreparedSyntaxLocation { |
| 45 | if existing := syntax_global_location(vm.owner, space, key) { |
| 46 | return PreparedSyntaxLocation{ |
| 47 | location: existing |
| 48 | name: if existing.name == name { existing.name } else { name.clone() } |
| 49 | } |
| 50 | } |
| 51 | created := &SyntaxGlobalLocation{ name: name.clone() } |
| 52 | if isnil(space) { |
| 53 | mut owner := vm.owner |
| 54 | owner.syntax_locations[key] = created |
| 55 | } else { |
| 56 | mut target := unsafe { &Namespace(space) } |
| 57 | target.syntax_locations[key] = created |
| 58 | } |
| 59 | return PreparedSyntaxLocation{ location: created, name: created.name, created: true } |
| 60 | } |
| 61 | |
| 62 | fn (mut vm VM) commit_syntax_location(space &Namespace, prepared PreparedSyntaxLocation, kind SyntaxBindingKind, slot int) { |
| 63 | mut owner := vm.owner |
| 64 | if !isnil(space) && space.syntax_identity == 0 { |
| 65 | mut target := unsafe { &Namespace(space) } |
| 66 | target.syntax_identity = vm.next_context() |
| 67 | } |
| 68 | if owner.syntax_generation < ~u64(0) { owner.syntax_generation++ } |
| 69 | mut location := prepared.location |
| 70 | location.kind = kind |
| 71 | location.name = prepared.name |
| 72 | if prepared.created && !isnil(space) { location.namespace = space.syntax_identity } |
| 73 | location.slot = slot |
| 74 | location.origin = unsafe { nil } |
| 75 | location.generation = owner.syntax_generation |
| 76 | } |
| 77 | |
| 78 | fn (mut vm VM) note_syntax_location(space &Namespace, name string, binding SyntaxBinding) { |
| 79 | if binding.kind == .global && !vm.owner.syntax_tracking { return } |
| 80 | prepared := vm.prepare_syntax_location(space, name, binding.name) |
| 81 | mut owner := vm.owner |
| 82 | owner.syntax_tracking = true |
| 83 | vm.commit_syntax_location(space, prepared, binding.kind, binding.global_slot) |
| 84 | } |
| 85 | |
| 86 | fn (mut vm VM) note_syntax_import(space &Namespace, name string, binding SyntaxBinding) { |
| 87 | // Variable imports already share fixed slots; syntax imports follow their |
| 88 | // provider's location, including a later replacement by a variable. |
| 89 | if binding.kind != .transformer { |
| 90 | vm.note_syntax_location(space, name, binding) |
| 91 | return |
| 92 | } |
| 93 | mut owner := vm.owner |
| 94 | owner.syntax_tracking = true |
| 95 | origin := syntax_global_location(owner, binding.namespace, binding.name) or { |
| 96 | vm.note_syntax_location(binding.namespace, binding.name, binding) |
| 97 | syntax_global_location(owner, binding.namespace, binding.name) or { panic('missing syntax location') } |
| 98 | } |
| 99 | canonical := origin.canonical() |
| 100 | if current := syntax_global_location(owner, space, name) { |
| 101 | if voidptr(current) == voidptr(canonical) { return } |
| 102 | } |
| 103 | vm.note_syntax_location(space, name, binding) |
| 104 | mut target := syntax_global_location(owner, space, name) or { panic('missing imported syntax location') } |
| 105 | target.origin = canonical |
| 106 | } |
| 107 | |
| 108 | struct RuntimeSyntaxBinding { |
| 109 | binding SyntaxBinding |
| 110 | namespace u64 |
| 111 | namespace_token bool = true |
| 112 | } |
| 113 | |
| 114 | fn runtime_syntax_namespace(space &Namespace) (u64, bool) { |
| 115 | if isnil(space) { return 0, true } |
| 116 | if space.syntax_identity != 0 { return space.syntax_identity, true } |
| 117 | // Offline/manual compiler fixtures have no VM coordinate. They retain their |
| 118 | // Namespace object, so pointer identity is safe and kept in a separate domain. |
| 119 | return u64(space), false |
| 120 | } |
| 121 | |
| 122 | fn syntax_location_binding(location &SyntaxGlobalLocation) RuntimeSyntaxBinding { |
| 123 | canonical := location.canonical() |
| 124 | return RuntimeSyntaxBinding{ |
| 125 | binding: SyntaxBinding{ |
| 126 | kind: canonical.kind |
| 127 | name: canonical.name |
| 128 | global_slot: canonical.slot |
| 129 | top_level: canonical.kind == .transformer |
| 130 | } |
| 131 | namespace: canonical.namespace |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | fn runtime_syntax_lookup(owner &Owner, space &Namespace, name string) ?RuntimeSyntaxBinding { |
| 136 | if isnil(owner) { return none } |
| 137 | if location := syntax_global_location(owner, space, name) { |
| 138 | return syntax_location_binding(location) |
| 139 | } |
| 140 | if isnil(space) { |
| 141 | if slot := owner.syntax_globals[name] { |
| 142 | return RuntimeSyntaxBinding{ binding: SyntaxBinding{ kind: .global, name: name, global_slot: slot } } |
| 143 | } |
| 144 | } else { |
| 145 | if slot := space.bindings[name] { |
| 146 | identity, token := runtime_syntax_namespace(space) |
| 147 | return RuntimeSyntaxBinding{ binding: SyntaxBinding{ kind: .global, name: name, namespace: space, global_slot: slot }, namespace: identity, namespace_token: token } |
| 148 | } |
| 149 | } |
| 150 | return none |
| 151 | } |
| 152 | |
| 153 | fn runtime_syntax_binding(owner &Owner, binding SyntaxBinding) RuntimeSyntaxBinding { |
| 154 | frozen := runtime_syntax_frozen(binding) |
| 155 | if binding.introduced != '' { |
| 156 | if private := runtime_syntax_lookup(owner, binding.introduced_namespace, binding.introduced) { |
| 157 | if private_syntax_location_allowed(owner, binding, private) { return private } |
| 158 | } |
| 159 | } |
| 160 | if binding.introduced_key != '' && binding.introduced_key != binding.introduced { |
| 161 | if private := runtime_syntax_lookup(owner, binding.introduced_namespace, binding.introduced_key) { |
| 162 | if private_syntax_named_location_allowed(owner, binding, binding.introduced_key, private) { |
| 163 | return private |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | if binding.kind == .lexical || (binding.kind == .transformer && !binding.top_level) { |
| 168 | return frozen |
| 169 | } |
| 170 | // Existing locations remain frozen across imports and unrelated later binds. |
| 171 | if binding.kind != .global || (!binding.unbound && binding.global_slot >= 0) { return frozen } |
| 172 | return runtime_syntax_lookup(owner, binding.namespace, binding.name) or { frozen } |
| 173 | } |
| 174 | |
| 175 | fn runtime_syntax_frozen(binding SyntaxBinding) RuntimeSyntaxBinding { |
| 176 | identity, token := runtime_syntax_namespace(binding.namespace) |
| 177 | return RuntimeSyntaxBinding{ binding: binding, namespace: identity, namespace_token: token } |
| 178 | } |
| 179 | |
| 180 | fn private_syntax_location_allowed(owner &Owner, binding SyntaxBinding, candidate RuntimeSyntaxBinding) bool { |
| 181 | return private_syntax_named_location_allowed(owner, binding, binding.introduced, candidate) |
| 182 | } |
| 183 | |
| 184 | fn syntax_declaration_is_new(owner &Owner, space &Namespace, name string, slot int, minimum_globals int, minimum_generation u64) bool { |
| 185 | if minimum_generation == 0 && minimum_globals < 0 { return true } |
| 186 | if minimum_generation > 0 { |
| 187 | if location := syntax_global_location(owner, space, name) { |
| 188 | // Inspect the local alias's birth, before following its older provider. |
| 189 | if location.generation >= minimum_generation { return true } |
| 190 | } |
| 191 | } |
| 192 | return minimum_globals >= 0 && slot >= minimum_globals |
| 193 | } |
| 194 | |
| 195 | fn private_syntax_named_location_allowed(owner &Owner, binding SyntaxBinding, name string, candidate RuntimeSyntaxBinding) bool { |
| 196 | if syntax_declaration_is_new(owner, binding.introduced_namespace, name, candidate.binding.global_slot, binding.introduced_globals, binding.introduced_generation) { |
| 197 | return true |
| 198 | } |
| 199 | if binding.kind == .transformer && candidate.binding.kind == .transformer && binding.top_level && candidate.binding.top_level { |
| 200 | frozen := runtime_syntax_frozen(binding) |
| 201 | return binding.name == candidate.binding.name && frozen.namespace == candidate.namespace && frozen.namespace_token == candidate.namespace_token |
| 202 | } |
| 203 | return same_syntax_binding(binding, candidate.binding) |
| 204 | } |
| 205 | |
| 206 | fn (code &Code) syntax_global_generation_floor() !u64 { |
| 207 | if isnil(code.compiler_vm) { return 0 } |
| 208 | mut owner := unsafe { (&VM(code.compiler_vm)).owner } |
| 209 | generation := owner.syntax_generation |
| 210 | if generation == ~u64(0) { return failure('syntax declaration generation limit exceeded') } |
| 211 | // Arm future definition recording with a scalar flag only. Capturing under a |
| 212 | // temporary host arena must never install a pointer allocated in that arena. |
| 213 | owner.syntax_tracking = true |
| 214 | return generation + 1 |
| 215 | } |
| 216 | |
| 217 | fn (mut b Builder) freeze_resolved_syntax_binding(name string) !SyntaxBinding { |
| 218 | binding := b.resolve_binding(name) |
| 219 | if isnil(b.code.generated) { return freeze_syntax_binding(binding) } |
| 220 | alias := b.code.generated.aliases[name] or { return freeze_syntax_binding(binding) } |
| 221 | if alias.use_site || !isnil(alias.capture_scope) { return freeze_syntax_binding(binding) } |
| 222 | key := if alias.capture_key != '' { alias.capture_key } else { name } |
| 223 | if local := scope_capture_binding(b.scope, key, alias.capture_scope, alias.capture_slots) { |
| 224 | if same_syntax_binding(local, binding) { return freeze_syntax_binding(binding) } |
| 225 | } |
| 226 | if binding.name in [name, key] && ((binding.kind == .global && !binding.unbound) || (binding.kind == .transformer && binding.top_level)) { |
| 227 | return freeze_syntax_binding(binding) |
| 228 | } |
| 229 | if binding.introduced != '' && binding.introduced != name { |
| 230 | return freeze_syntax_binding(binding) |
| 231 | } |
| 232 | frozen := freeze_syntax_binding(binding)! |
| 233 | return SyntaxBinding{ |
| 234 | ...frozen |
| 235 | introduced: name |
| 236 | introduced_key: if key != name { key } else { '' } |
| 237 | introduced_namespace: b.code.namespace |
| 238 | introduced_globals: alias.capture_globals |
| 239 | introduced_generation: alias.capture_generation |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | fn runtime_syntax_binding_equal(left SyntaxBinding, right SyntaxBinding, left_owner &Owner, right_owner &Owner) bool { |
| 244 | a := runtime_syntax_binding(left_owner, left) |
| 245 | b := runtime_syntax_binding(right_owner, right) |
| 246 | if ((a.binding.kind == .global && b.binding.kind == .global && (!a.binding.unbound || !b.binding.unbound)) |
| 247 | || (a.binding.kind == .transformer && b.binding.kind == .transformer)) |
| 248 | && voidptr(left_owner) != voidptr(right_owner) { |
| 249 | return false |
| 250 | } |
| 251 | if a.binding.kind == .transformer && b.binding.kind == .transformer && a.binding.top_level && b.binding.top_level { |
| 252 | return a.binding.name == b.binding.name && a.namespace == b.namespace && a.namespace_token == b.namespace_token |
| 253 | } |
| 254 | return same_syntax_binding(a.binding, b.binding) |
| 255 | } |
| 256 | |