module scheme enum SyntaxBindingKind as u8 { global lexical keyword transformer } // Lexical references name a function's binding location, never its current // value. This preserves assignment and works through extra compile-time regions // that share a single runtime activation. struct SyntaxBinding { pattern_variable &SyntaxCaseVariable = unsafe { nil } kind SyntaxBindingKind lexical_id u32 name string function &Function = unsafe { nil } slot int transformer &SyntaxTransformer = unsafe { nil } top_level bool introduced string introduced_key string introduced_globals int = -1 introduced_generation u64 namespace &Namespace = unsafe { nil } introduced_namespace &Namespace = unsafe { nil } global_slot int = -1 assignable bool unbound bool lexical_domain &SyntaxLexicalDomain = unsafe { nil } } struct IdentifierAlias { context &SyntaxLookupContext = unsafe { nil } capture_key string capture_scope &Scope = unsafe { nil } capture_slots int capture_globals int = -1 capture_generation u64 binding SyntaxBinding use_site bool use_scope &Scope = unsafe { nil } use_name string wrap &SyntaxWrap = unsafe { nil } } fn same_syntax_binding(a SyntaxBinding, b SyntaxBinding) bool { if a.kind != b.kind { return false } if a.kind == .lexical { if isnil(a.pattern_variable) != isnil(b.pattern_variable) { return false } if !isnil(a.pattern_variable) && a.pattern_variable.variable != b.pattern_variable.variable { return false } return a.slot == b.slot && same_lexical_syntax_function(a, b) } if a.kind == .transformer { return if a.top_level && b.top_level { a.name == b.name && voidptr(a.namespace) == voidptr(b.namespace) } else { voidptr(a.transformer) == voidptr(b.transformer) } } if a.kind == .global { if a.unbound || b.unbound { return a.unbound && b.unbound && a.name == b.name } if same := same_report_binding(a, b) { return same } if a.global_slot >= 0 && b.global_slot >= 0 { return a.global_slot == b.global_slot } return a.name == b.name && voidptr(a.namespace) == voidptr(b.namespace) } return a.name == b.name } // Report environments are one immutable set of locations per VM, even when a // shorter caller scope needs an uncached namespace wrapper. Explicit imports // may have materialized the same original location as an absolute VM slot. fn same_report_binding(a SyntaxBinding, b SyntaxBinding) ?bool { a_report := report_slot(a.namespace, a.global_slot) b_report := report_slot(b.namespace, b.global_slot) if !a_report && !b_report { return none } if a_report && b_report { return voidptr(a.namespace.report_owner) == voidptr(b.namespace.report_owner) && a.global_slot == b.global_slot } if a_report { if slot := a.namespace.report_owner.report_slots[-a.global_slot - 2] { return slot == b.global_slot } } else { if slot := b.namespace.report_owner.report_slots[-b.global_slot - 2] { return slot == a.global_slot } } return false } fn scope_binding(scope &Scope, name string) ?SyntaxBinding { mut cursor := ScopeRef{scope} for !isnil(cursor.value) { if slot := cursor.value.names[name] { return SyntaxBinding{ kind: .lexical, name: name, function: cursor.value.function, slot: slot } } if !isnil(cursor.value.syntax) { if binding := cursor.value.syntax.entries[name] { return binding } } cursor = ScopeRef{cursor.value.parent} } return none } fn (b &Builder) resolve_binding(name string) SyntaxBinding { // Reader lowering uses one reserved compiler form. Its printed spelling // must retain the same identity through lexical variables and transformers. if name in [implicit_statement_name, 'vile:statement'] { return SyntaxBinding{ kind: .keyword, name: implicit_statement_name } } if binding := scope_binding(b.scope, name) { return binding } if !isnil(b.code.generated) { if binding := b.code.generated.globals[name] { return binding } if alias := b.code.generated.aliases[name] { // A staged sibling can inherit an alias before the declaration's compiler // canonicalizes it. Resolve the same marked binder key in either child. capture_key := if alias.capture_key != '' { alias.capture_key } else { b.code.binding_key(name) } if capture_key != '' { if binding := scope_capture_binding(b.scope, capture_key, alias.capture_scope, alias.capture_slots) { return binding } if binding := b.code.generated.globals[capture_key] { return binding } if b.known_global(capture_key) { candidate := b.global_syntax_binding(capture_key) if private_syntax_location_allowed(b.syntax_owner(), SyntaxBinding{ ...alias.binding introduced: capture_key introduced_namespace: b.code.namespace introduced_globals: alias.capture_globals introduced_generation: alias.capture_generation }, runtime_syntax_frozen(candidate)) { return candidate } } } // Exact private aliases installed by a declaration retain its location // across staged namespace compilation, including captured identifiers. if b.known_global(name) { return b.global_syntax_binding(name) } if alias.use_site { if binding := scope_binding(alias.use_scope, alias.use_name) { return binding } if binding := b.code.generated.globals[alias.use_name] { return binding } } binding := b.current_syntax_binding(alias.binding) return if !alias.use_site && binding.kind == .global && binding.introduced == '' { SyntaxBinding{ ...binding introduced: name introduced_key: if capture_key != name { capture_key } else { '' } introduced_namespace: b.code.namespace introduced_globals: alias.capture_globals introduced_generation: alias.capture_generation } } else { binding } } } return b.global_syntax_binding(name) } fn (b &Builder) current_syntax_binding(binding SyntaxBinding) SyntaxBinding { // Selected record auxiliary exports use canonical syntax identities rather // than executable namespace spellings. Preserve them through exported macros. if binding.kind == .keyword && (binding.name.starts_with('record-aux:') || binding.name in [ implicit_statement_name, 'vile:statement', ]) { return binding } if binding.introduced != '' && b.known_global_in(binding.introduced_namespace, binding.introduced) { candidate := b.global_syntax_binding_in(binding.introduced_namespace, binding.introduced) if private_syntax_location_allowed(b.syntax_owner(), binding, runtime_syntax_frozen(candidate)) { return candidate } } if binding.introduced_key != '' && binding.introduced_key != binding.introduced && b.known_global_in(binding.introduced_namespace, binding.introduced_key) { candidate := b.global_syntax_binding_in(binding.introduced_namespace, binding.introduced_key) if private_syntax_named_location_allowed(b.syntax_owner(), binding, binding.introduced_key, runtime_syntax_frozen(candidate)) { return candidate } } if binding.kind == .lexical || (binding.kind == .transformer && !binding.top_level) { return binding } // Existing variable references identify locations. Imports can rebind the // same spelling without changing a frozen macro's definition-site binding. if binding.kind == .global && binding.global_slot >= 0 { return binding } resolved := b.global_syntax_binding_in(binding.namespace, binding.name) return if binding.introduced != '' && resolved.kind == .global { SyntaxBinding{ ...resolved introduced: binding.introduced introduced_key: binding.introduced_key introduced_namespace: binding.introduced_namespace introduced_globals: binding.introduced_globals introduced_generation: binding.introduced_generation } } else { resolved } } fn (b &Builder) known_global(name string) bool { return b.known_global_in(b.code.namespace, name) } fn (b &Builder) known_global_in(space &Namespace, name string) bool { if voidptr(space) != voidptr(b.code.namespace) { return !isnil(b.code.compiler_vm) && unsafe { (&VM(b.code.compiler_vm)).namespace_has_binding(space, name) } } if !isnil(b.code.generated) && name in b.code.generated.globals { return true } syntax_table := b.current_syntax_table() if !isnil(syntax_table) && name in syntax_table.entries { return true } return !isnil(b.code.known_globals) && name in b.code.known_globals } fn (b &Builder) global_syntax_binding(name string) SyntaxBinding { return b.global_syntax_binding_in(b.code.namespace, name) } fn (b &Builder) global_syntax_binding_in(space &Namespace, name string) SyntaxBinding { if voidptr(space) != voidptr(b.code.namespace) { if !isnil(b.code.compiler_vm) { return unsafe { (&VM(b.code.compiler_vm)).namespace_binding(space, name) } } return SyntaxBinding{ kind: .global, name: name, namespace: space, unbound: true } } if !isnil(b.code.generated) { if binding := b.code.generated.globals[name] { return binding } } return b.base_global_syntax_binding(name) } // The immutable base excludes this compiler's staged declaration overlay. fn (b &Builder) base_global_syntax_binding(name string) SyntaxBinding { space := b.code.namespace syntax_table := b.current_syntax_table() if !isnil(syntax_table) { if binding := syntax_table.entries[name] { return if !isnil(b.code.compiler_vm) { unsafe { (&VM(b.code.compiler_vm)).imported_syntax_binding(space, name, binding) } } else { binding } } } if !isnil(b.code.known_globals) { if slot := (*b.code.known_globals)[name] { assignable := if !isnil(b.code.compiler_vm) { unsafe { (&VM(b.code.compiler_vm)).assignment_permitted_in(space, name) } } else { isnil(space) || space.writable } return SyntaxBinding{ kind: .global, name: name, namespace: space, global_slot: slot, assignable: assignable } } } return if name in [implicit_statement_name, 'vile:statement'] || ((isnil(space) || space.implicit_syntax) && special_name(name) && name !in b.code.keyword_variables) { SyntaxBinding{ kind: .keyword, name: name, namespace: space } } else { SyntaxBinding{ kind: .global, name: name, namespace: space, unbound: true } } } fn (b &Builder) current_syntax_table() &SyntaxBindings { if !isnil(b.code.syntax_base) { return b.code.syntax_base } // A native reentry can install the first syntax table after this Code was // created. Keep prepared/offline tables authoritative; only official live // namespace borrows observe that newly allocated table. if !b.cacheable_syntax_globals() { return unsafe { nil } } vm := unsafe { &VM(b.code.compiler_vm) } return if isnil(b.code.namespace) { vm.syntax } else { b.code.namespace.syntax } } fn (b &Builder) binding_depth(function &Function) !int { mut cursor := ScopeRef{b.scope} mut depth := 0 for !isnil(cursor.value) { if voidptr(cursor.value.function) == voidptr(function) { return depth } parent := cursor.value.parent if !isnil(parent) && voidptr(parent.function) != voidptr(cursor.value.function) { depth++ } cursor = ScopeRef{parent} } return failure('lexical identifier is outside its binding region') } fn (mut code Code) identifier_alias(alias IdentifierAlias) !string { mut arena := code.syntax_arena() if arena.compiled { return failure('cannot change compiled syntax') } arena.serial++ name := '\x00${code.syntax_seed}.${arena.serial}' installed := if alias.capture_globals >= 0 && alias.capture_generation == 0 { IdentifierAlias{ ...alias, capture_generation: code.syntax_global_generation_floor()! } } else { alias } arena.set_alias(name, installed) return name } fn (mut b Builder) declare_definition(name string) int { if !isnil(b.code.generated) { if alias := b.code.generated.aliases[name] { if (alias.use_site && voidptr(alias.use_scope) == voidptr(b.scope)) || (alias.capture_key != '' && isnil(alias.wrap) && alias.use_name != '') { slot := b.scope.declare(alias.use_name) b.scope.names[name] = slot if alias.capture_key != '' { b.scope.names[alias.capture_key] = slot } return slot } } } return b.scope.declare(name) } fn (mut b Builder) global_definition_name(name string) string { mut target := name if isnil(b.code.generated) && !isnil(b.code.syntax_base) && name in b.code.syntax_base.entries { // A variable definition must mask its previous transformer while its // initializer compiles, before runtime updates the borrowed VM table. b.code.syntax_arena() } if !isnil(b.code.generated) { if alias := b.code.generated.aliases[name] { if (alias.use_site && isnil(alias.use_scope)) || (alias.capture_key != '' && isnil(alias.wrap) && alias.use_name != '') { target = alias.use_name } if !alias.use_site && alias.capture_key != '' && !isnil(alias.wrap) { target = alias.capture_key } } binding := SyntaxBinding{ kind: .global, name: target, namespace: b.code.namespace } b.code.generated.set_global_binding(name, binding) b.code.generated.set_global_binding(target, binding) } if special_name(target) && target !in b.code.keyword_variables { b.code.keyword_variables << target } return target } fn scope_capture_binding(scope &Scope, name string, boundary &Scope, slots int) ?SyntaxBinding { mut cursor := ScopeRef{scope} for !isnil(cursor.value) { current := cursor.value at_boundary := voidptr(current) == voidptr(boundary) if slot := current.names[name] { if !at_boundary || slot >= slots { return SyntaxBinding{ kind: .lexical, name: name, function: current.function, slot: slot } } } if at_boundary { if !isnil(current.syntax) { if binding := current.syntax.entries[name] { if binding.kind == .transformer && !isnil(binding.transformer.enumeration) { hidden := binding.transformer.enumeration.binding if hidden.kind == .lexical && hidden.slot >= slots && syntax_binding_function_matches(hidden, current.function) { return binding } } } } return none } if !isnil(current.syntax) { if binding := current.syntax.entries[name] { return binding } } cursor = ScopeRef{current.parent} } return none } // Semantic binding equality deliberately ignores alias spelling and namespace // permissions. Transporting one occurrence must preserve those fields too. fn same_syntax_transport(a SyntaxBinding, b SyntaxBinding) bool { return same_syntax_binding(a, b) && a.name == b.name && a.slot == b.slot && a.global_slot == b.global_slot && a.assignable == b.assignable && a.unbound == b.unbound && a.top_level == b.top_level && a.introduced == b.introduced && voidptr(a.namespace) == voidptr(b.namespace) && a.introduced_globals == b.introduced_globals && a.introduced_generation == b.introduced_generation && a.introduced_key == b.introduced_key && voidptr(a.introduced_namespace) == voidptr(b.introduced_namespace) && voidptr(a.function) == voidptr(b.function) && a.lexical_id == b.lexical_id && voidptr(a.lexical_domain) == voidptr(b.lexical_domain) && voidptr(a.transformer) == voidptr(b.transformer) && voidptr(a.pattern_variable) == voidptr(b.pattern_variable) } fn (b &Builder) identifier_context(name string) &SyntaxLookupContext { if !isnil(b.code.generated) { if alias := b.code.generated.aliases[name] { return alias.context } } return unsafe { nil } }