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
use crate::{
semantic_analysis::ast_node::TypedExpression, type_system::*, CallPath, CompileResult, Ident,
TypedDeclaration, TypedFunctionDeclaration,
};
use super::{module::Module, root::Root, submodule_namespace::SubmoduleNamespace, Path, PathBuf};
use sway_types::span::Span;
use std::collections::VecDeque;
#[derive(Clone, Debug, PartialEq)]
pub struct Namespace {
init: Module,
pub(crate) root: Root,
pub(crate) mod_path: PathBuf,
}
impl Namespace {
pub fn init_root(init: Module) -> Self {
let root = Root::from(init.clone());
let mod_path = vec![];
Self {
init,
root,
mod_path,
}
}
pub fn mod_path(&self) -> &Path {
&self.mod_path
}
pub fn find_module_path<'a, T>(&'a self, prefixes: T) -> PathBuf
where
T: IntoIterator<Item = &'a Ident>,
{
self.mod_path.iter().chain(prefixes).cloned().collect()
}
pub fn root(&self) -> &Root {
&self.root
}
pub fn root_mut(&mut self) -> &mut Root {
&mut self.root
}
pub fn module(&self) -> &Module {
&self.root.module[&self.mod_path]
}
pub fn module_mut(&mut self) -> &mut Module {
&mut self.root.module[&self.mod_path]
}
pub(crate) fn resolve_symbol(&self, symbol: &Ident) -> CompileResult<&TypedDeclaration> {
self.root.resolve_symbol(&self.mod_path, symbol)
}
pub(crate) fn resolve_call_path(
&self,
call_path: &CallPath,
) -> CompileResult<&TypedDeclaration> {
self.root.resolve_call_path(&self.mod_path, call_path)
}
pub(crate) fn resolve_type_with_self(
&mut self,
type_id: TypeId,
self_type: TypeId,
span: &Span,
enforce_type_arguments: EnforceTypeArguments,
type_info_prefix: Option<&Path>,
) -> CompileResult<TypeId> {
self.root.resolve_type_with_self(
type_id,
self_type,
span,
enforce_type_arguments,
type_info_prefix,
&self.mod_path,
)
}
pub(crate) fn resolve_type_without_self(
&mut self,
type_id: TypeId,
span: &Span,
type_info_prefix: Option<&Path>,
) -> CompileResult<TypeId> {
self.root.resolve_type(
type_id,
span,
EnforceTypeArguments::Yes,
type_info_prefix,
&self.mod_path,
)
}
pub(crate) fn find_method_for_type(
&mut self,
r#type: TypeId,
method_prefix: &Path,
method_name: &Ident,
self_type: TypeId,
args_buf: &VecDeque<TypedExpression>,
) -> CompileResult<TypedFunctionDeclaration> {
self.root.find_method_for_type(
&self.mod_path,
r#type,
method_prefix,
method_name,
self_type,
args_buf,
)
}
pub(crate) fn star_import(&mut self, src: &Path) -> CompileResult<()> {
self.root.star_import(src, &self.mod_path)
}
pub(crate) fn self_import(&mut self, src: &Path, alias: Option<Ident>) -> CompileResult<()> {
self.root.self_import(src, &self.mod_path, alias)
}
pub(crate) fn item_import(
&mut self,
src: &Path,
item: &Ident,
alias: Option<Ident>,
) -> CompileResult<()> {
self.root.item_import(src, item, &self.mod_path, alias)
}
pub(crate) fn enter_submodule(&mut self, dep_name: Ident) -> SubmoduleNamespace {
let init = self.init.clone();
self.submodules.entry(dep_name.to_string()).or_insert(init);
let submod_path: Vec<_> = self
.mod_path
.iter()
.cloned()
.chain(Some(dep_name))
.collect();
let parent_mod_path = std::mem::replace(&mut self.mod_path, submod_path);
SubmoduleNamespace {
namespace: self,
parent_mod_path,
}
}
}
impl std::ops::Deref for Namespace {
type Target = Module;
fn deref(&self) -> &Self::Target {
self.module()
}
}
impl std::ops::DerefMut for Namespace {
fn deref_mut(&mut self) -> &mut Self::Target {
self.module_mut()
}
}