vile / scheme / syntax_use_context.v
131 lines · 120 sloc · 4.42 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import strconv
4
5// Keep only identifier spellings and private alias labels from normalization.
6// These contexts contain no Code, Scope, VM, reader arena or expanded tree.
7@[heap]
8struct SyntaxUseContext {
9 seed u64
10 first u64
11 last u64
12 namespace_id u64
13mut:
14 names map[string]string
15}
16
17@[heap]
18struct SyntaxUseRanges {
19mut:
20 by_first map[u64]&SyntaxUseContext
21 ordered []&SyntaxUseContext
22 dirty bool
23}
24
25@[heap]
26struct SyntaxUseContexts {
27mut:
28 seeds map[u64]&SyntaxUseRanges
29}
30
31struct SyntaxUseCoordinate {
32 seed u64
33 serial u64
34}
35
36// Alias labels are already owned, small decimal coordinates. Borrow their two
37// components and use the stdlib's checked, allocation-free integer parser.
38fn syntax_use_coordinate(name string) ?SyntaxUseCoordinate {
39 if name.len < 4 || name[0] != 0 { return none }
40 dot := name.index_u8(`.`)
41 if dot < 2 || dot == name.len - 1 { return none }
42 seed, seed_error := strconv.common_parse_uint2(text_view(name, 1, dot), 10, 64)
43 serial, serial_error := strconv.common_parse_uint2(text_view(name, dot + 1, name.len), 10, 64)
44 if seed_error != 0 || serial_error != 0 { return none }
45 return SyntaxUseCoordinate{seed, serial}
46}
47
48fn (mut code Code) syntax_namespace_identity() u64 {
49 if isnil(code.namespace) { return 0 }
50 mut space := unsafe { &Namespace(code.namespace) }
51 if space.syntax_identity == 0 {
52 if code.syntax_seed == 0 { panic('missing namespace syntax coordinate') }
53 space.syntax_identity = code.syntax_seed
54 }
55 return space.syntax_identity
56}
57
58fn (mut code Code) keep_syntax_use_context(seed u64, first u64, last u64, namespace_id u64, names map[string]string) &SyntaxUseContext {
59 mut arena := code.syntax_arena()
60 if isnil(arena.use_contexts) { arena.use_contexts = &SyntaxUseContexts{} }
61 mut contexts := arena.use_contexts
62 mut ranges := contexts.seeds[seed] or {
63 created := &SyntaxUseRanges{}
64 contexts.seeds[seed] = created
65 created
66 }
67 if mut existing := ranges.by_first[first] {
68 // Authored map iteration copies string keys in this V compiler. The
69 // common repeated registration only borrows the same identity map.
70 if names.len == 0 || existing.names == names { return existing }
71 for name, alias in names { existing.names[name] = alias }
72 return existing
73 }
74 context := &SyntaxUseContext{ seed: seed, first: first, last: last, namespace_id: namespace_id, names: names }
75 ranges.by_first[first] = context
76 if ranges.ordered.len > 0 && ranges.ordered.last().first > first { ranges.dirty = true }
77 ranges.ordered << context
78 return context
79}
80
81fn (mut code Code) syntax_use_context(alias string) ?&SyntaxUseContext {
82 if isnil(code.generated) || isnil(code.generated.use_contexts) { return none }
83 coordinate := syntax_use_coordinate(alias) or { return none }
84 mut ranges := code.generated.use_contexts.seeds[coordinate.seed] or { return none }
85 if ranges.dirty {
86 ranges.ordered.sort(a.first < b.first)
87 ranges.dirty = false
88 }
89 mut low := 0
90 mut high := ranges.ordered.len
91 for low < high {
92 middle := low + (high - low) / 2
93 if ranges.ordered[middle].last < coordinate.serial {
94 low = middle + 1
95 } else {
96 high = middle
97 }
98 }
99 if low < ranges.ordered.len {
100 context := ranges.ordered[low]
101 if coordinate.serial >= context.first { return context }
102 }
103 return none
104}
105
106// Importers retain only entries for aliases reached by that imported subtree.
107// Source context pointers are temporary keys here, never fields on Code.
108@[heap]
109struct SyntaxUseImportState {
110mut:
111 contexts map[voidptr]&SyntaxUseContext
112}
113
114fn (mut importer SourceImporter) import_use_context(name string, alias IdentifierAlias) {
115 if !alias.use_site { return }
116 context := importer.parent.syntax_use_context(name) or { return }
117 if context.names[alias.use_name] or { '' } != name { return }
118 if isnil(importer.use_contexts) { importer.use_contexts = &SyntaxUseImportState{} }
119 mut state := importer.use_contexts
120 mut target := state.contexts[voidptr(context)] or {
121 // A staged driver has already detached its owned body. Its forms must
122 // share helper identities with siblings which compile later. Declaration
123 // importers instead filter entries to their reachable body subtrees.
124 names := if importer.share_use_contexts { context.names } else { map[string]string{} }
125 created := importer.code.keep_syntax_use_context(context.seed, context.first, context.last, context.namespace_id, names)
126 state.contexts[voidptr(context)] = created
127 created
128 }
129 if importer.share_use_contexts { return }
130 target.names[alias.use_name] = name
131}
132