module scheme import arena // A captured lookup context can resolve identifiers that were absent from the // original input. It contains locations and detached lexical frames, never a // compiler, scope, function, VM, or any runtime global values. Namespace and // transformer metadata still require their publication owner to remain alive. @[heap] struct SyntaxLookupContext { temporaries &SyntaxTemporaryContext = unsafe { nil } origin &SyntaxLookupContext = unsafe { nil } overlay_frames int overlay_boundary bool overlay_slots int lexical &SyntaxLexicalFrame = unsafe { nil } globals map[string]SyntaxBinding overrides map[string]SyntaxBinding private_bindings &SyntaxPrivateSnapshot = unsafe { nil } global_floor int = -1 global_generation u64 namespace &Namespace = unsafe { nil } implicit_syntax bool keyword_variables []string } fn (mut b Builder) capture_syntax_lookup_context() !&SyntaxLookupContext { snapshot := b.capture_base_syntax_globals()! private_bindings := if b.cacheable_syntax_globals() { b.capture_private_syntax_bindings()! } else { unsafe { &SyntaxPrivateSnapshot(nil) } } mut overrides := map[string]SyntaxBinding{} if !b.cacheable_syntax_globals() && !isnil(b.code.generated) { reserve_syntax_binding_map(mut overrides, b.code.generated.aliases.len + b.code.generated.globals.len) for name, _ in b.code.generated.aliases { // Exact generated names participate in lookup too; resolve captures // and declaration aliases before detaching compiler-only locations. overrides[name] = own_context_binding(b.freeze_resolved_syntax_binding(name)!, name)! } for name, _ in b.code.generated.globals { // A staged declaration masks an earlier syntax binding/location. overrides[name] = own_context_binding(b.global_syntax_binding(name), name)! } } return &SyntaxLookupContext{ lexical: capture_lexical_syntax_context(b.scope)! globals: unsafe { snapshot.bindings } overrides: overrides private_bindings: private_bindings global_floor: b.syntax_global_count() global_generation: b.code.syntax_global_generation_floor()! namespace: b.code.namespace implicit_syntax: isnil(b.code.namespace) || b.code.namespace.implicit_syntax keyword_variables: b.code.keyword_variables.map(it.clone()) } } // Map iteration already owns the lookup key. Canonical imported names can // differ from that key, so retain an owned spelling for those as well. fn own_context_binding(input SyntaxBinding, key string) !SyntaxBinding { binding := freeze_syntax_binding(input)! return SyntaxBinding{ ...binding name: if binding.name == key { key } else { binding.name.clone() } introduced: if binding.introduced.len > 0 { binding.introduced.clone() } else { '' } introduced_key: if binding.introduced_key.len > 0 { binding.introduced_key.clone() } else { '' } } } fn (context &SyntaxLookupContext) binding(name string) SyntaxBinding { if name in [implicit_statement_name, 'vile:statement'] { return SyntaxBinding{ kind: .keyword, name: implicit_statement_name } } if binding := lexical_syntax_context_binding(context.lexical, name) { return binding } if binding := context.overrides[name] { return binding } if !isnil(context.private_bindings) { if binding := context.private_bindings.binding(name) { return binding } } if binding := context.globals[name] { return binding } return if context.implicit_syntax && special_name(name) && name !in context.keyword_variables { SyntaxBinding{ kind: .keyword, name: name, namespace: context.namespace } } else { SyntaxBinding{ kind: .global, name: name, namespace: context.namespace, unbound: true } } } struct SyntaxLookupContextRef { value &SyntaxLookupContext } // An origin overlay adds only substitutions born after its anchor was emitted. // Older caller bindings must never replace the anchor's definition-site context. fn (context &SyntaxLookupContext) binding_with_marks(name string, history &SyntaxWrap) SyntaxBinding { wrap := if !isnil(history) && history.antimark { history.parent } else { history } if isnil(wrap) && isnil(context.origin) { return context.binding(name) } if isnil(context.origin) && !isnil(context.temporaries) { if binding := context.temporaries.binding(name, wrap) { return binding } } storage := arena.new(chunk_size: 1024) defer { release_frozen_syntax_scratch(storage) } key := syntax_marked_lookup_key(name, wrap, storage) mut cursor := SyntaxLookupContextRef{context} for !isnil(cursor.value) { current := cursor.value mut frames := SyntaxLexicalFrameRef{current.lexical} if isnil(current.origin) { if !isnil(current.temporaries) { if binding := current.temporaries.binding(name, wrap) { return binding } } for !isnil(frames.value) { if binding := frames.value.marked_binding(key, name, -1) { return binding } frames = SyntaxLexicalFrameRef{frames.value.parent} } return if isnil(wrap) { current.binding(name) } else { current.marked_global_binding(name, key) } } for _ in 0 .. current.overlay_frames { if isnil(frames.value) { break } if binding := frames.value.marked_binding(key, name, -1) { return binding } if isnil(wrap) { if binding := frames.value.marked_binding(name, name, -1) { return binding } } frames = SyntaxLexicalFrameRef{frames.value.parent} } if current.overlay_boundary && !isnil(frames.value) { if binding := frames.value.marked_binding(key, name, current.overlay_slots) { return binding } if isnil(wrap) { if binding := frames.value.marked_binding(name, name, current.overlay_slots) { return binding } } } cursor = SyntaxLookupContextRef{current.origin} } return context.binding(name) } fn syntax_marked_lookup_key(name string, wrap &SyntaxWrap, storage arena.Arena) string { family := syntax_capture_spelling(wrap, storage, true) storage.push() defer { storage.pop() } return '${family}/${external_identifier_spelling(name)}' } // A datum can introduce any spelling with the anchor's context, including one // absent from the original template. Resolve the marked global spelling before // the public spelling, and carry a declaration fence if it is still pending. // The scratch lookup key must never escape in the returned binding. fn (context &SyntaxLookupContext) marked_global_binding(name string, key string) SyntaxBinding { if binding := context.overrides[key] { return binding } if !isnil(context.private_bindings) { if binding := context.private_bindings.binding(key) { return binding } } if binding := context.globals[key] { return binding } binding := context.binding(name) if !isnil(context.temporaries) || binding.kind == .lexical || (binding.kind == .transformer && !binding.top_level) { return binding } return SyntaxBinding{ ...binding introduced: key.clone() introduced_key: '' introduced_namespace: context.namespace introduced_globals: context.global_floor introduced_generation: context.global_generation } } fn (frame &SyntaxLexicalFrame) marked_binding(key string, owned_name string, first_slot int) ?SyntaxBinding { if slot := frame.names[key] { if slot >= first_slot { // The lookup key can live in scratch storage; the binding retains only the // caller-owned spelling and detached lexical location. return SyntaxBinding{ kind: .lexical, name: owned_name, slot: slot, lexical_domain: frame.domain, lexical_id: frame.function_id } } } if binding := frame.syntax[key] { if first_slot < 0 { return binding } if binding.kind == .transformer && !isnil(binding.transformer) && !isnil(binding.transformer.enumeration) { hidden := binding.transformer.enumeration.binding if hidden.kind == .lexical && hidden.slot >= first_slot && hidden.lexical_id == frame.function_id && voidptr(hidden.lexical_domain) == voidptr(frame.domain) { return binding } } } return none } // Only the immutable map is published. Cache identity and invalidation fields // remain with the compiler and never enter a syntax object's ownership graph. struct SyntaxGlobalSnapshot { bindings map[string]SyntaxBinding } struct SyntaxGlobalCache { owner &Owner namespace &Namespace = unsafe { nil } syntax_table &SyntaxBindings = unsafe { nil } epoch u64 writable bool snapshot &SyntaxGlobalSnapshot } fn (mut b Builder) capture_base_syntax_globals() !&SyntaxGlobalSnapshot { cacheable := b.cacheable_syntax_globals() if cacheable && isnil(b.code.syntax_base) { vm := unsafe { &VM(b.code.compiler_vm) } b.code.syntax_base = if isnil(b.code.namespace) { vm.syntax } else { b.code.namespace.syntax } if !isnil(b.code.syntax_base) && b.code.syntax_base.entries.len > 0 { b.code.has_syntax = true } } epoch := if cacheable { unsafe { (&VM(b.code.compiler_vm)).binding_epoch } } else { u64(0) } writable := isnil(b.code.namespace) || b.code.namespace.writable if cacheable && !isnil(b.code.procedural) && !isnil(b.code.procedural.globals_cache) { prior := b.code.procedural.globals_cache 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 { return prior.snapshot } } mut globals := map[string]SyntaxBinding{} estimated := (if isnil(b.code.known_globals) { 0 } else { b.code.known_globals.len }) + (if isnil(b.code.syntax_base) { 0 } else { b.code.syntax_base.entries.len }) reserve_syntax_binding_map(mut globals, estimated) if !isnil(b.code.known_globals) { for name, _ in *b.code.known_globals { globals[name] = own_context_binding(b.base_global_syntax_binding(name), name)! } } if !isnil(b.code.syntax_base) { for name, _ in b.code.syntax_base.entries { if name !in globals { globals[name] = own_context_binding(b.base_global_syntax_binding(name), name)! } } } snapshot := &SyntaxGlobalSnapshot{ bindings: globals } if cacheable { mut metadata := b.code.procedural_syntax() metadata.globals_cache = &SyntaxGlobalCache{ owner: b.syntax_owner() namespace: b.code.namespace syntax_table: b.code.syntax_base epoch: epoch writable: writable snapshot: snapshot } } return snapshot } fn (b &Builder) cacheable_syntax_globals() bool { if isnil(b.code.compiler_vm) || isnil(b.code.known_globals) { return false } vm := unsafe { &VM(b.code.compiler_vm) } expected := if isnil(b.code.namespace) { vm.bindings } else { &b.code.namespace.bindings } live_syntax := if isnil(b.code.namespace) { vm.syntax } else { b.code.namespace.syntax } return voidptr(b.code.known_globals) == voidptr(expected) && (isnil(b.code.syntax_base) || voidptr(b.code.syntax_base) == voidptr(live_syntax)) } // Arena storage retains old map growth slabs. The input tables provide a cheap // upper bound; use the stdlib reservation rather than accumulating those copies. fn reserve_syntax_binding_map(mut bindings map[string]SyntaxBinding, capacity int) { if capacity > 0 && u64(capacity) <= u64(~u32(0)) { bindings.reserve(u32(capacity)) } }