module scheme import reader @[heap] struct RecordGlobalAliases { mut: names map[string]bool } // Only record declarations transported into another namespace need permanent // private aliases. Ordinary definitions keep their existing one-name behavior. fn (mut b Builder) mark_record_global_aliases(declaration RecordDeclaration) { if !isnil(b.scope) { return } mut records := b.code.generated.records for name in declaration.bindings { alias := b.code.generated.aliases[name] or { continue } if !alias.use_site || !isnil(alias.use_scope) { continue } context := b.code.syntax_use_context(name) or { continue } if context.namespace_id == b.code.syntax_namespace_identity() { continue } if isnil(records.aliases) { records.aliases = &RecordGlobalAliases{} } records.aliases.names[name] = true } } fn (mut b Builder) record_global_alias_operand(name string) !int { if isnil(b.code.generated) || isnil(b.code.generated.records) || isnil(b.code.generated.records.aliases) { return 0 } if name !in b.code.generated.records.aliases.names { return 0 } return global_alias_operand(b.name_index(name)) } fn global_alias_operand(index int) !int { if index < 0 || index > 2147483647 { return failure('global alias exceeds supported operand range') } return -index - 1 } @[inline] fn global_alias_index(operand i32) int { return -int(operand) - 1 } // Introduced names share an expansion-family prefix. A deterministic suffix // lets shorthand declarations meet sibling references across staged begins // without retaining an expansion arena or a name registry. fn (mut b Builder) derive_record_name(source reader.NodeId, raw string) !string { name := b.code.name(source)! // Shorthand operations use public spellings too. Encode a leading NUL once // before lookup/definition so it cannot impersonate a hygienic private key. public_key := external_identifier_key(raw) mut arena := b.code.syntax_arena() if alias := arena.aliases[name] { if alias.use_site { // The capture group distinguishes lexical and namespace contexts even // when both scopes are nil. Reuse the caller's exact helper identity // before considering a newly derived spelling. if context := b.code.syntax_use_context(name) { if context.names[alias.use_name] or { '' } == name { if captured := context.names[public_key] { if captured !in arena.aliases { binding := scope_binding(alias.use_scope, public_key) or { SyntaxBinding{ kind: .global, name: public_key, namespace: alias.binding.namespace, unbound: true } } arena.set_alias(captured, IdentifierAlias{ binding: binding, use_site: true, use_scope: alias.use_scope, use_name: public_key, wrap: alias.wrap }) } return captured } } } if voidptr(alias.use_scope) == voidptr(b.scope) { return public_key } binding := scope_binding(alias.use_scope, public_key) or { SyntaxBinding{ kind: .global, name: public_key, namespace: alias.binding.namespace, unbound: true } } return b.code.identifier_alias(IdentifierAlias{ binding: binding, use_site: true, use_scope: alias.use_scope, use_name: public_key, wrap: alias.wrap }) } family := if alias.capture_key != '' { // Derived helpers have a third namespace, separate from actual numeric // labels and canonical ~h keys. Raw names cannot impersonate either. captured_family := record_introduction_family(alias.capture_key) '\x00~d' + captured_family.substr_unsafe(3, captured_family.len) } else { record_introduction_family(name) } if family != '' { key := '${family}/${raw}' if key !in arena.aliases { arena.set_alias(key, IdentifierAlias{ binding: SyntaxBinding{ kind: .global, name: public_key, namespace: alias.binding.namespace, unbound: true } wrap: alias.wrap capture_key: if alias.capture_key != '' { record_introduction_family(alias.capture_key) + '/' + raw } else { '' } }) } return key } } return public_key } fn (b &Builder) record_definition_key(key string) string { if !isnil(b.code.generated) { if alias := b.code.generated.aliases[key] { if alias.use_site && voidptr(alias.use_scope) == voidptr(b.scope) { return alias.use_name } } } return key } fn (mut b Builder) template_record_identifier(info SyntaxIdentifier, context i32, mut state SyntaxExpansion) !string { mut family := '' previous_marks := same_syntax_marks(state.family_wrap, info.wrap) if state.family != '' && previous_marks && state.family_context == context { family = state.family } else if state.family != '' && previous_marks { if !isnil(state.families) { family = state.families.groups[context] or { '' } } } else if state.family != '' && !isnil(state.wrapped_families) { family = state.wrapped_families.family(context, info.wrap) } mut name := '' if family == '' { mut arena := b.code.syntax_arena() arena.serial++ name = '\x00${b.code.syntax_seed}.${arena.serial}/${info.raw_name}' // Both immutable handles start at the same owned key allocation. Keeping // its prefix avoids allocating a family string and copying it into this // first key; escaped context handles keep the original allocation alive. family = name.substr_unsafe(0, name.len - info.raw_name.len - 1) if state.family == '' { state.family = family state.family_context = context state.family_wrap = info.wrap } else if previous_marks { if isnil(state.families) { state.families = &RecordExpansionGroups{} } state.families.groups[context] = family } else { if isnil(state.wrapped_families) { state.wrapped_families = &RecordWrappedFamilies{} } state.wrapped_families.keep(context, info.wrap, family) } } else { name = '${family}/${info.raw_name}' } if isnil(state.domain) { state.domain = b.syntax_mark_domain() } wrap := state.transcription_wrap(info.wrap) mut capture_key := '' if prior := b.code.generated.aliases[name] { if !same_syntax_transport(prior.binding, info.binding) || voidptr(prior.context) != voidptr(info.context) { capture_key = b.code.binding_key(name) name = b.code.identifier_alias(IdentifierAlias{ binding: info.binding, wrap: wrap, capture_key: capture_key })! } else { capture_key = prior.capture_key } } b.code.generated.set_alias(name, IdentifierAlias{ context: info.context use_name: external_identifier_key(info.raw_name) binding: info.binding wrap: wrap capture_key: capture_key capture_scope: b.scope capture_slots: if isnil(b.scope) { 0 } else { b.scope.function.slot_count } capture_globals: b.syntax_global_count() capture_generation: b.code.syntax_global_generation_floor()! }) return name } @[heap] struct RecordExpansionGroups { mut: groups map[i32]string } // A template key already carries its introduction family. This prefix view // uses that immutable storage; aliases need no duplicate family string field. fn record_introduction_family(name string) string { if name.len == 0 || name[0] != 0 { return '' } end := name.index_u8(`/`) if end < 0 { return '' } return name.substr_unsafe(0, end) } @[heap] struct RecordTreeContexts { mut: groups map[i32]i32 } fn (tree &SyntaxTree) record_context(identifier i32) i32 { if isnil(tree.record_contexts) { return 0 } return tree.record_contexts.groups[identifier] or { 0 } } // Only non-default contexts allocate these temporary interning maps. Frozen // trees keep local integers, never a scope pointer or source-family storage. @[heap] struct RecordFreezeGroups { mut: introductions map[string]i32 scopes map[voidptr]i32 next i32 } fn (mut freezer SyntaxFreezer) freeze_record_context(mut b Builder, name string) i32 { if isnil(b.code.generated) { return 0 } if alias := b.code.generated.aliases[name] { if alias.use_site && voidptr(alias.use_scope) == voidptr(b.scope) { return 0 } family := if alias.use_site { '' } else { record_introduction_family(name) } if !alias.use_site && family == '' { return 0 } if isnil(freezer.scope_contexts) { freezer.scope_contexts = &RecordFreezeGroups{} } mut groups := freezer.scope_contexts if alias.use_site { if previous := groups.scopes[voidptr(alias.use_scope)] { return previous } groups.next++ groups.scopes[voidptr(alias.use_scope)] = groups.next } else { if previous := groups.introductions[family] { return previous } groups.next++ groups.introductions[family] = groups.next } return groups.next } return 0 } struct RecordWrappedFamily { context i32 wrap &SyntaxWrap family string } @[heap] struct RecordWrappedFamilies { mut: buckets map[u64][]RecordWrappedFamily } fn (families &RecordWrappedFamilies) family(context i32, wrap &SyntaxWrap) string { key := record_wrap_key(context, wrap) for entry in families.buckets[key] or { return '' } { if entry.context == context && same_syntax_marks(entry.wrap, wrap) { return entry.family } } return '' } fn (mut families RecordWrappedFamilies) keep(context i32, wrap &SyntaxWrap, family string) { key := record_wrap_key(context, wrap) mut bucket := families.buckets[key] or { []RecordWrappedFamily{} } bucket << RecordWrappedFamily{context, wrap, family} families.buckets[key] = bucket } // Hash the positive mark set, ignoring order and repeated entries, exactly // like same_syntax_marks. Equality still checks every collision. No temporary // set or cache is allocated on the ordinary nil-wrap path. fn record_wrap_key(context i32, history &SyntaxWrap) u64 { mut result := u64(0) mut cursor := SyntaxWrapRef{history} for !isnil(cursor.value) { if !syntax_mark_present(cursor.value.parent, cursor.value.domain, cursor.value.mark) { result ^= hash_word(hash_word(0x9e3779b97f4a7c15, u64(cursor.value.domain)), cursor.value.mark) } cursor = SyntaxWrapRef{cursor.value.parent} } return hash_word(result, u64(u32(context))) }