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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use std::collections::HashMap;
use crate::{AliasId, DeclId, ModuleId, OverlayId, VarId};
pub static DEFAULT_OVERLAY_NAME: &str = "zero";
#[derive(Debug, Clone)]
pub struct Visibility {
decl_ids: HashMap<DeclId, bool>,
alias_ids: HashMap<AliasId, bool>,
}
impl Visibility {
pub fn new() -> Self {
Visibility {
decl_ids: HashMap::new(),
alias_ids: HashMap::new(),
}
}
pub fn is_decl_id_visible(&self, decl_id: &DeclId) -> bool {
*self.decl_ids.get(decl_id).unwrap_or(&true)
}
pub fn is_alias_id_visible(&self, alias_id: &AliasId) -> bool {
*self.alias_ids.get(alias_id).unwrap_or(&true)
}
pub fn hide_decl_id(&mut self, decl_id: &DeclId) {
self.decl_ids.insert(*decl_id, false);
}
pub fn hide_alias_id(&mut self, alias_id: &AliasId) {
self.alias_ids.insert(*alias_id, false);
}
pub fn use_decl_id(&mut self, decl_id: &DeclId) {
self.decl_ids.insert(*decl_id, true);
}
pub fn use_alias_id(&mut self, alias_id: &AliasId) {
self.alias_ids.insert(*alias_id, true);
}
pub fn merge_with(&mut self, other: Visibility) {
self.decl_ids.extend(other.decl_ids);
self.alias_ids.extend(other.alias_ids);
}
pub fn append(&mut self, other: &Visibility) {
for (decl_id, visible) in other.decl_ids.iter() {
if !self.decl_ids.contains_key(decl_id) {
self.decl_ids.insert(*decl_id, *visible);
}
}
for (alias_id, visible) in other.alias_ids.iter() {
if !self.alias_ids.contains_key(alias_id) {
self.alias_ids.insert(*alias_id, *visible);
}
}
}
}
#[derive(Debug, Clone)]
pub struct ScopeFrame {
pub overlays: Vec<(Vec<u8>, OverlayFrame)>,
pub active_overlays: Vec<OverlayId>,
pub removed_overlays: Vec<Vec<u8>>,
pub predecls: HashMap<Vec<u8>, DeclId>,
}
impl ScopeFrame {
pub fn new() -> Self {
Self {
overlays: vec![],
active_overlays: vec![],
removed_overlays: vec![],
predecls: HashMap::new(),
}
}
pub fn with_empty_overlay(name: Vec<u8>, origin: ModuleId) -> Self {
Self {
overlays: vec![(name, OverlayFrame::from_origin(origin))],
active_overlays: vec![0],
removed_overlays: vec![],
predecls: HashMap::new(),
}
}
pub fn get_var(&self, var_name: &[u8]) -> Option<&VarId> {
for overlay_id in self.active_overlays.iter().rev() {
if let Some(var_id) = self
.overlays
.get(*overlay_id)
.expect("internal error: missing overlay")
.1
.vars
.get(var_name)
{
return Some(var_id);
}
}
None
}
pub fn active_overlay_ids(&self, removed_overlays: &mut Vec<Vec<u8>>) -> Vec<OverlayId> {
for name in &self.removed_overlays {
if !removed_overlays.contains(name) {
removed_overlays.push(name.clone());
}
}
self.active_overlays
.iter()
.filter(|id| !removed_overlays.contains(self.get_overlay_name(**id)))
.copied()
.collect()
}
pub fn active_overlays(&self, removed_overlays: &mut Vec<Vec<u8>>) -> Vec<&OverlayFrame> {
self.active_overlay_ids(removed_overlays)
.iter()
.map(|id| self.get_overlay(*id))
.collect()
}
pub fn active_overlay_names(&self, removed_overlays: &mut Vec<Vec<u8>>) -> Vec<&Vec<u8>> {
self.active_overlay_ids(removed_overlays)
.iter()
.map(|id| self.get_overlay_name(*id))
.collect()
}
pub fn get_overlay_name(&self, overlay_id: OverlayId) -> &Vec<u8> {
&self
.overlays
.get(overlay_id)
.expect("internal error: missing overlay")
.0
}
pub fn get_overlay(&self, overlay_id: OverlayId) -> &OverlayFrame {
&self
.overlays
.get(overlay_id)
.expect("internal error: missing overlay")
.1
}
pub fn get_overlay_mut(&mut self, overlay_id: OverlayId) -> &mut OverlayFrame {
&mut self
.overlays
.get_mut(overlay_id)
.expect("internal error: missing overlay")
.1
}
pub fn find_overlay(&self, name: &[u8]) -> Option<OverlayId> {
self.overlays.iter().position(|(n, _)| n == name)
}
pub fn find_active_overlay(&self, name: &[u8]) -> Option<OverlayId> {
self.overlays
.iter()
.position(|(n, _)| n == name)
.and_then(|id| {
if self.active_overlays.contains(&id) {
Some(id)
} else {
None
}
})
}
}
#[derive(Debug, Clone)]
pub struct OverlayFrame {
pub vars: HashMap<Vec<u8>, VarId>,
pub predecls: HashMap<Vec<u8>, DeclId>,
pub decls: HashMap<Vec<u8>, DeclId>,
pub aliases: HashMap<Vec<u8>, AliasId>,
pub modules: HashMap<Vec<u8>, ModuleId>,
pub visibility: Visibility,
pub origin: ModuleId,
}
impl OverlayFrame {
pub fn from_origin(origin: ModuleId) -> Self {
Self {
vars: HashMap::new(),
predecls: HashMap::new(),
decls: HashMap::new(),
aliases: HashMap::new(),
modules: HashMap::new(),
visibility: Visibility::new(),
origin,
}
}
}