| 1 | module scheme |
| 2 | |
| 3 | import reader |
| 4 | |
| 5 | @[heap] |
| 6 | struct RecordGlobalAliases { |
| 7 | mut: |
| 8 | names map[string]bool |
| 9 | } |
| 10 | |
| 11 | // Only record declarations transported into another namespace need permanent |
| 12 | // private aliases. Ordinary definitions keep their existing one-name behavior. |
| 13 | fn (mut b Builder) mark_record_global_aliases(declaration RecordDeclaration) { |
| 14 | if !isnil(b.scope) { return } |
| 15 | mut records := b.code.generated.records |
| 16 | for name in declaration.bindings { |
| 17 | alias := b.code.generated.aliases[name] or { continue } |
| 18 | if !alias.use_site || !isnil(alias.use_scope) { continue } |
| 19 | context := b.code.syntax_use_context(name) or { continue } |
| 20 | if context.namespace_id == b.code.syntax_namespace_identity() { continue } |
| 21 | if isnil(records.aliases) { records.aliases = &RecordGlobalAliases{} } |
| 22 | records.aliases.names[name] = true |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | fn (mut b Builder) record_global_alias_operand(name string) !int { |
| 27 | if isnil(b.code.generated) || isnil(b.code.generated.records) || isnil(b.code.generated.records.aliases) { |
| 28 | return 0 |
| 29 | } |
| 30 | if name !in b.code.generated.records.aliases.names { return 0 } |
| 31 | return global_alias_operand(b.name_index(name)) |
| 32 | } |
| 33 | |
| 34 | fn global_alias_operand(index int) !int { |
| 35 | if index < 0 || index > 2147483647 { |
| 36 | return failure('global alias exceeds supported operand range') |
| 37 | } |
| 38 | return -index - 1 |
| 39 | } |
| 40 | |
| 41 | @[inline] |
| 42 | fn global_alias_index(operand i32) int { |
| 43 | return -int(operand) - 1 |
| 44 | } |
| 45 | |
| 46 | // Introduced names share an expansion-family prefix. A deterministic suffix |
| 47 | // lets shorthand declarations meet sibling references across staged begins |
| 48 | // without retaining an expansion arena or a name registry. |
| 49 | fn (mut b Builder) derive_record_name(source reader.NodeId, raw string) !string { |
| 50 | name := b.code.name(source)! |
| 51 | // Shorthand operations use public spellings too. Encode a leading NUL once |
| 52 | // before lookup/definition so it cannot impersonate a hygienic private key. |
| 53 | public_key := external_identifier_key(raw) |
| 54 | mut arena := b.code.syntax_arena() |
| 55 | if alias := arena.aliases[name] { |
| 56 | if alias.use_site { |
| 57 | // The capture group distinguishes lexical and namespace contexts even |
| 58 | // when both scopes are nil. Reuse the caller's exact helper identity |
| 59 | // before considering a newly derived spelling. |
| 60 | if context := b.code.syntax_use_context(name) { |
| 61 | if context.names[alias.use_name] or { '' } == name { |
| 62 | if captured := context.names[public_key] { |
| 63 | if captured !in arena.aliases { |
| 64 | binding := scope_binding(alias.use_scope, public_key) or { SyntaxBinding{ kind: .global, name: public_key, namespace: alias.binding.namespace, unbound: true } } |
| 65 | arena.set_alias(captured, IdentifierAlias{ binding: binding, use_site: true, use_scope: alias.use_scope, use_name: public_key, wrap: alias.wrap }) |
| 66 | } |
| 67 | return captured |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | if voidptr(alias.use_scope) == voidptr(b.scope) { return public_key } |
| 72 | binding := scope_binding(alias.use_scope, public_key) or { SyntaxBinding{ kind: .global, name: public_key, namespace: alias.binding.namespace, unbound: true } } |
| 73 | return b.code.identifier_alias(IdentifierAlias{ binding: binding, use_site: true, use_scope: alias.use_scope, use_name: public_key, wrap: alias.wrap }) |
| 74 | } |
| 75 | family := if alias.capture_key != '' { |
| 76 | // Derived helpers have a third namespace, separate from actual numeric |
| 77 | // labels and canonical ~h keys. Raw names cannot impersonate either. |
| 78 | captured_family := record_introduction_family(alias.capture_key) |
| 79 | '\x00~d' + captured_family.substr_unsafe(3, captured_family.len) |
| 80 | } else { |
| 81 | record_introduction_family(name) |
| 82 | } |
| 83 | if family != '' { |
| 84 | key := '${family}/${raw}' |
| 85 | if key !in arena.aliases { |
| 86 | arena.set_alias(key, IdentifierAlias{ |
| 87 | binding: SyntaxBinding{ kind: .global, name: public_key, namespace: alias.binding.namespace, unbound: true } |
| 88 | wrap: alias.wrap |
| 89 | capture_key: if alias.capture_key != '' { |
| 90 | record_introduction_family(alias.capture_key) + '/' + raw |
| 91 | } else { |
| 92 | '' |
| 93 | } |
| 94 | }) |
| 95 | } |
| 96 | return key |
| 97 | } |
| 98 | } |
| 99 | return public_key |
| 100 | } |
| 101 | |
| 102 | fn (b &Builder) record_definition_key(key string) string { |
| 103 | if !isnil(b.code.generated) { |
| 104 | if alias := b.code.generated.aliases[key] { |
| 105 | if alias.use_site && voidptr(alias.use_scope) == voidptr(b.scope) { |
| 106 | return alias.use_name |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | return key |
| 111 | } |
| 112 | |
| 113 | fn (mut b Builder) template_record_identifier(info SyntaxIdentifier, context i32, mut state SyntaxExpansion) !string { |
| 114 | mut family := '' |
| 115 | previous_marks := same_syntax_marks(state.family_wrap, info.wrap) |
| 116 | if state.family != '' && previous_marks && state.family_context == context { |
| 117 | family = state.family |
| 118 | } else if state.family != '' && previous_marks { |
| 119 | if !isnil(state.families) { family = state.families.groups[context] or { '' } } |
| 120 | } else if state.family != '' && !isnil(state.wrapped_families) { |
| 121 | family = state.wrapped_families.family(context, info.wrap) |
| 122 | } |
| 123 | mut name := '' |
| 124 | if family == '' { |
| 125 | mut arena := b.code.syntax_arena() |
| 126 | arena.serial++ |
| 127 | name = '\x00${b.code.syntax_seed}.${arena.serial}/${info.raw_name}' |
| 128 | // Both immutable handles start at the same owned key allocation. Keeping |
| 129 | // its prefix avoids allocating a family string and copying it into this |
| 130 | // first key; escaped context handles keep the original allocation alive. |
| 131 | family = name.substr_unsafe(0, name.len - info.raw_name.len - 1) |
| 132 | if state.family == '' { |
| 133 | state.family = family |
| 134 | state.family_context = context |
| 135 | state.family_wrap = info.wrap |
| 136 | } else if previous_marks { |
| 137 | if isnil(state.families) { state.families = &RecordExpansionGroups{} } |
| 138 | state.families.groups[context] = family |
| 139 | } else { |
| 140 | if isnil(state.wrapped_families) { state.wrapped_families = &RecordWrappedFamilies{} } |
| 141 | state.wrapped_families.keep(context, info.wrap, family) |
| 142 | } |
| 143 | } else { |
| 144 | name = '${family}/${info.raw_name}' |
| 145 | } |
| 146 | if isnil(state.domain) { state.domain = b.syntax_mark_domain() } |
| 147 | wrap := state.transcription_wrap(info.wrap) |
| 148 | mut capture_key := '' |
| 149 | if prior := b.code.generated.aliases[name] { |
| 150 | if !same_syntax_transport(prior.binding, info.binding) || voidptr(prior.context) != voidptr(info.context) { |
| 151 | capture_key = b.code.binding_key(name) |
| 152 | name = b.code.identifier_alias(IdentifierAlias{ binding: info.binding, wrap: wrap, capture_key: capture_key })! |
| 153 | } else { |
| 154 | capture_key = prior.capture_key |
| 155 | } |
| 156 | } |
| 157 | b.code.generated.set_alias(name, IdentifierAlias{ |
| 158 | context: info.context |
| 159 | use_name: external_identifier_key(info.raw_name) |
| 160 | binding: info.binding |
| 161 | wrap: wrap |
| 162 | capture_key: capture_key |
| 163 | capture_scope: b.scope |
| 164 | capture_slots: if isnil(b.scope) { 0 } else { b.scope.function.slot_count } |
| 165 | capture_globals: b.syntax_global_count() |
| 166 | capture_generation: b.code.syntax_global_generation_floor()! |
| 167 | }) |
| 168 | return name |
| 169 | } |
| 170 | |
| 171 | @[heap] |
| 172 | struct RecordExpansionGroups { |
| 173 | mut: |
| 174 | groups map[i32]string |
| 175 | } |
| 176 | |
| 177 | // A template key already carries its introduction family. This prefix view |
| 178 | // uses that immutable storage; aliases need no duplicate family string field. |
| 179 | fn record_introduction_family(name string) string { |
| 180 | if name.len == 0 || name[0] != 0 { return '' } |
| 181 | end := name.index_u8(`/`) |
| 182 | if end < 0 { return '' } |
| 183 | return name.substr_unsafe(0, end) |
| 184 | } |
| 185 | |
| 186 | @[heap] |
| 187 | struct RecordTreeContexts { |
| 188 | mut: |
| 189 | groups map[i32]i32 |
| 190 | } |
| 191 | |
| 192 | fn (tree &SyntaxTree) record_context(identifier i32) i32 { |
| 193 | if isnil(tree.record_contexts) { return 0 } |
| 194 | return tree.record_contexts.groups[identifier] or { 0 } |
| 195 | } |
| 196 | |
| 197 | // Only non-default contexts allocate these temporary interning maps. Frozen |
| 198 | // trees keep local integers, never a scope pointer or source-family storage. |
| 199 | @[heap] |
| 200 | struct RecordFreezeGroups { |
| 201 | mut: |
| 202 | introductions map[string]i32 |
| 203 | scopes map[voidptr]i32 |
| 204 | next i32 |
| 205 | } |
| 206 | |
| 207 | fn (mut freezer SyntaxFreezer) freeze_record_context(mut b Builder, name string) i32 { |
| 208 | if isnil(b.code.generated) { return 0 } |
| 209 | if alias := b.code.generated.aliases[name] { |
| 210 | if alias.use_site && voidptr(alias.use_scope) == voidptr(b.scope) { return 0 } |
| 211 | family := if alias.use_site { '' } else { record_introduction_family(name) } |
| 212 | if !alias.use_site && family == '' { return 0 } |
| 213 | if isnil(freezer.scope_contexts) { freezer.scope_contexts = &RecordFreezeGroups{} } |
| 214 | mut groups := freezer.scope_contexts |
| 215 | if alias.use_site { |
| 216 | if previous := groups.scopes[voidptr(alias.use_scope)] { return previous } |
| 217 | groups.next++ |
| 218 | groups.scopes[voidptr(alias.use_scope)] = groups.next |
| 219 | } else { |
| 220 | if previous := groups.introductions[family] { return previous } |
| 221 | groups.next++ |
| 222 | groups.introductions[family] = groups.next |
| 223 | } |
| 224 | return groups.next |
| 225 | } |
| 226 | return 0 |
| 227 | } |
| 228 | |
| 229 | struct RecordWrappedFamily { |
| 230 | context i32 |
| 231 | wrap &SyntaxWrap |
| 232 | family string |
| 233 | } |
| 234 | |
| 235 | @[heap] |
| 236 | struct RecordWrappedFamilies { |
| 237 | mut: |
| 238 | buckets map[u64][]RecordWrappedFamily |
| 239 | } |
| 240 | |
| 241 | fn (families &RecordWrappedFamilies) family(context i32, wrap &SyntaxWrap) string { |
| 242 | key := record_wrap_key(context, wrap) |
| 243 | for entry in families.buckets[key] or { return '' } { |
| 244 | if entry.context == context && same_syntax_marks(entry.wrap, wrap) { return entry.family } |
| 245 | } |
| 246 | return '' |
| 247 | } |
| 248 | |
| 249 | fn (mut families RecordWrappedFamilies) keep(context i32, wrap &SyntaxWrap, family string) { |
| 250 | key := record_wrap_key(context, wrap) |
| 251 | mut bucket := families.buckets[key] or { []RecordWrappedFamily{} } |
| 252 | bucket << RecordWrappedFamily{context, wrap, family} |
| 253 | families.buckets[key] = bucket |
| 254 | } |
| 255 | |
| 256 | // Hash the positive mark set, ignoring order and repeated entries, exactly |
| 257 | // like same_syntax_marks. Equality still checks every collision. No temporary |
| 258 | // set or cache is allocated on the ordinary nil-wrap path. |
| 259 | fn record_wrap_key(context i32, history &SyntaxWrap) u64 { |
| 260 | mut result := u64(0) |
| 261 | mut cursor := SyntaxWrapRef{history} |
| 262 | for !isnil(cursor.value) { |
| 263 | if !syntax_mark_present(cursor.value.parent, cursor.value.domain, cursor.value.mark) { |
| 264 | result ^= hash_word(hash_word(0x9e3779b97f4a7c15, u64(cursor.value.domain)), cursor.value.mark) |
| 265 | } |
| 266 | cursor = SyntaxWrapRef{cursor.value.parent} |
| 267 | } |
| 268 | return hash_word(result, u64(u32(context))) |
| 269 | } |
| 270 | |