vile / scheme / library_import_names.v
59 lines · 52 sloc · 1.6 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3// Only a prefix creates fresh names in the transform pass. Provider and
4// explicit rename names are borrowed. A full prefix needs no per-row sidecar;
5// materialize flags only when filtering/renaming divides that ownership.
6struct ImportNameOwnership {
7mut:
8 all bool
9 flags []bool
10}
11
12fn import_selection_name_flags(capacity int) []bool {
13 mut flags := []bool{cap: capacity}
14 unsafe { flags.flags.set(.noslices) }
15 return flags
16}
17
18@[inline]
19fn (names &ImportNameOwnership) owns(index int) bool {
20 return names.all || (names.flags.len > 0 && names.flags[index])
21}
22
23fn (mut names ImportNameOwnership) discard(index int, name string, count int) {
24 if !names.owns(index) { return }
25 if names.all {
26 names.flags = filled_storage(count, true)
27 names.all = false
28 }
29 container_store_validated(mut names.flags, index, false)
30 mut owned := name
31 unsafe { owned.free() }
32}
33
34fn (mut names ImportNameOwnership) release_metadata() {
35 unsafe { names.flags.free() }
36 names.flags = []bool{}
37 names.all = false
38}
39
40fn (mut names ImportNameOwnership) release[T](items []T) {
41 if names.all || names.flags.len > 0 {
42 for i, item in items {
43 if names.owns(i) {
44 mut owned := item.name
45 unsafe { owned.free() }
46 }
47 }
48 }
49 names.release_metadata()
50}
51
52fn prefixed_import_name(prefix string, name string) string {
53 // The pinned concat implementation always allocates, even for an empty
54 // operand. Encoding a leading NUL makes another fresh allocation.
55 mut joined := prefix + external_identifier_spelling(name)
56 encoded := external_identifier_key(joined)
57 if encoded.str != joined.str { unsafe { joined.free() } }
58 return encoded
59}
60