1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use std::{cell::RefCell, convert::TryInto, iter::Iterator};
use indexmap::map::IndexMap;
use crate::{environment::slots::ModuleSlotId, syntax::ast::Visibility};
#[derive(Debug)]
pub(crate) struct MutableNames(RefCell<IndexMap<String, (ModuleSlotId, Visibility)>>);
#[derive(Debug)]
pub(crate) struct FrozenNames(IndexMap<String, (ModuleSlotId, Visibility)>);
impl MutableNames {
pub fn new() -> Self {
Self(RefCell::new(IndexMap::new()))
}
pub fn slot_count(&self) -> u32 {
self.0.borrow().len().try_into().unwrap()
}
pub fn get_slot(&self, slot: ModuleSlotId) -> Option<String> {
for (s, (i, _vis)) in &*self.0.borrow() {
if *i == slot {
return Some(s.clone());
}
}
None
}
pub(crate) fn get_name(&self, name: &str) -> Option<(ModuleSlotId, Visibility)> {
self.0.borrow().get(name).copied()
}
pub(crate) fn add_name_visibility(&self, name: &str, vis: Visibility) -> ModuleSlotId {
let mut x = self.0.borrow_mut();
match x.get_mut(name) {
Some((slot, stored_vis)) => {
if *stored_vis == Visibility::Private {
*stored_vis = vis;
}
*slot
}
None => {
let slot = ModuleSlotId::new(x.len().try_into().unwrap());
x.insert(name.to_owned(), (slot, vis));
slot
}
}
}
pub fn add_name(&self, name: &str) -> ModuleSlotId {
self.add_name_visibility(name, Visibility::Public)
}
pub fn hide_name(&self, name: &str) {
self.0.borrow_mut().remove(name);
}
pub fn all_names(&self) -> IndexMap<String, ModuleSlotId> {
self.0
.borrow()
.iter()
.map(|(name, (slot, _vis))| (name.to_owned(), *slot))
.collect()
}
pub(crate) fn all_names_and_visibilities(&self) -> IndexMap<String, Visibility> {
self.0
.borrow()
.iter()
.map(|(name, (_slot, vis))| (name.to_owned(), *vis))
.collect()
}
pub fn freeze(self) -> FrozenNames {
FrozenNames(self.0.into_inner())
}
}
impl FrozenNames {
pub(crate) fn get_name(&self, name: &str) -> Option<(ModuleSlotId, Visibility)> {
self.0.get(name).copied()
}
pub(crate) fn all_symbols(&self) -> impl Iterator<Item = (&str, ModuleSlotId)> {
self.0
.iter()
.map(|(name, (slot, _vis))| (name.as_str(), *slot))
}
pub fn symbols(&self) -> impl Iterator<Item = (&str, ModuleSlotId)> {
self.0.iter().filter_map(|(name, (slot, vis))| match vis {
Visibility::Private => None,
Visibility::Public => Some((name.as_str(), *slot)),
})
}
}