sway_core/semantic_analysis/namespace/
module.rs

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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use crate::{
    engine_threading::Engines,
    language::{
        ty::{self},
        Visibility,
    },
    Ident, TypeId,
};

use super::{
    lexical_scope::{Items, LexicalScope, ResolvedFunctionDecl},
    LexicalScopeId, ModuleName, ModulePath, ModulePathBuf, ResolvedDeclaration,
    ResolvedTraitImplItem, TraitMap,
};

use rustc_hash::FxHasher;
use std::{collections::HashMap, hash::BuildHasherDefault};
use sway_error::handler::Handler;
use sway_error::{error::CompileError, handler::ErrorEmitted};
use sway_types::{span::Span, Spanned};

/// A single `Module` within a Sway project.
///
/// A `Module` is most commonly associated with an individual file of Sway code, e.g. a top-level
/// script/predicate/contract file or some library dependency whether introduced via `mod` or the
/// `[dependencies]` table of a `forc` manifest.
///
/// A `Module` contains a set of all items that exist within the lexical scope via declaration or
/// importing, along with a map of each of its submodules.
#[derive(Clone, Debug)]
pub struct Module {
    /// Submodules of the current module represented as an ordered map from each submodule's name
    /// to the associated `Module`.
    ///
    /// Submodules are normally introduced in Sway code with the `mod foo;` syntax where `foo` is
    /// some library dependency that we include as a submodule.
    ///
    /// Note that we *require* this map to produce deterministic codegen results which is why [`FxHasher`] is used.
    submodules: im::HashMap<ModuleName, Module, BuildHasherDefault<FxHasher>>,
    /// Keeps all lexical scopes associated with this module.
    pub lexical_scopes: Vec<LexicalScope>,
    /// Current lexical scope id in the lexical scope hierarchy stack.
    pub current_lexical_scope_id: LexicalScopeId,
    /// Maps between a span and the corresponding lexical scope id.
    pub lexical_scopes_spans: HashMap<Span, LexicalScopeId>,
    /// Name of the module, package name for root module, module name for other modules.
    /// Module name used is the same as declared in `mod name;`.
    name: Ident,
    /// Whether or not this is a `pub` module
    visibility: Visibility,
    /// Empty span at the beginning of the file implementing the module
    span: Option<Span>,
    /// An absolute path from the `root` that represents the module location.
    ///
    /// The path of the root module in a package is `[package_name]`. If a module `X` is a submodule
    /// of module `Y` which is a submodule of the root module in the package `P`, then the path is
    /// `[P, Y, X]`.
    mod_path: ModulePathBuf,
}

impl Module {
    pub(super) fn new(
        name: Ident,
        visibility: Visibility,
        span: Option<Span>,
        parent_mod_path: &ModulePathBuf,
    ) -> Self {
        let mut mod_path = parent_mod_path.clone();
        mod_path.push(name.clone());
        Self {
            visibility,
            submodules: Default::default(),
            lexical_scopes: vec![LexicalScope::default()],
            lexical_scopes_spans: Default::default(),
            current_lexical_scope_id: 0,
            name,
            span,
            mod_path,
        }
    }

    pub fn name(&self) -> &Ident {
        &self.name
    }

    pub fn visibility(&self) -> &Visibility {
        &self.visibility
    }

    pub fn span(&self) -> &Option<Span> {
        &self.span
    }

    pub fn set_span(&mut self, span: Span) {
        self.span = Some(span);
    }

    pub(super) fn add_new_submodule(
        &mut self,
        name: &Ident,
        visibility: Visibility,
        span: Option<Span>,
    ) {
        let module = Self::new(name.clone(), visibility, span, &self.mod_path);
        self.submodules.insert(name.to_string(), module);
    }

    pub fn read<R>(&self, _engines: &crate::Engines, mut f: impl FnMut(&Module) -> R) -> R {
        f(self)
    }

    pub fn write<R>(
        &mut self,
        _engines: &crate::Engines,
        mut f: impl FnMut(&mut Module) -> R,
    ) -> R {
        f(self)
    }

    pub fn mod_path(&self) -> &ModulePath {
        self.mod_path.as_slice()
    }

    pub fn mod_path_buf(&self) -> ModulePathBuf {
        self.mod_path.clone()
    }

    /// Immutable access to this module's submodules.
    pub fn submodules(&self) -> &im::HashMap<ModuleName, Module, BuildHasherDefault<FxHasher>> {
        &self.submodules
    }

    pub fn has_submodule(&self, name: &Ident) -> bool {
        self.submodule(&[name.clone()]).is_some()
    }

    /// Mutable access to this module's submodules.
    pub fn submodules_mut(
        &mut self,
    ) -> &mut im::HashMap<ModuleName, Module, BuildHasherDefault<FxHasher>> {
        &mut self.submodules
    }

    /// Lookup the submodule at the given path.
    pub fn submodule(&self, path: &ModulePath) -> Option<&Module> {
        let mut module = self;
        for ident in path.iter() {
            match module.submodules.get(ident.as_str()) {
                Some(ns) => module = ns,
                None => return None,
            }
        }
        Some(module)
    }

    /// Unique access to the submodule at the given path.
    pub fn submodule_mut(&mut self, path: &ModulePath) -> Option<&mut Module> {
        let mut module = self;
        for ident in path.iter() {
            match module.submodules.get_mut(ident.as_str()) {
                Some(ns) => module = ns,
                None => return None,
            }
        }
        Some(module)
    }

    /// Lookup the submodule at the given path.
    ///
    /// This should be used rather than `Index` when we don't yet know whether the module exists.
    pub(crate) fn lookup_submodule(
        &self,
        handler: &Handler,
        path: &[Ident],
    ) -> Result<&Module, ErrorEmitted> {
        match self.submodule(path) {
            None => Err(handler.emit_err(module_not_found(path, true))),
            Some(module) => Ok(module),
        }
    }

    /// Returns the root lexical scope id associated with this module.
    pub fn root_lexical_scope_id(&self) -> LexicalScopeId {
        0
    }

    /// Returns the root lexical scope associated with this module.
    pub fn root_lexical_scope(&self) -> &LexicalScope {
        self.lexical_scopes
            .get(self.root_lexical_scope_id())
            .unwrap()
    }

    pub fn get_lexical_scope(&self, id: LexicalScopeId) -> Option<&LexicalScope> {
        self.lexical_scopes.get(id)
    }

    pub fn get_lexical_scope_mut(&mut self, id: LexicalScopeId) -> Option<&mut LexicalScope> {
        self.lexical_scopes.get_mut(id)
    }

    /// Returns the current lexical scope associated with this module.
    pub fn current_lexical_scope(&self) -> &LexicalScope {
        self.lexical_scopes
            .get(self.current_lexical_scope_id)
            .unwrap()
    }

    /// Returns the mutable current lexical scope associated with this module.
    pub fn current_lexical_scope_mut(&mut self) -> &mut LexicalScope {
        self.lexical_scopes
            .get_mut(self.current_lexical_scope_id)
            .unwrap()
    }

    /// The collection of items declared by this module's current lexical scope.
    pub fn current_items(&self) -> &Items {
        &self.current_lexical_scope().items
    }

    /// The collection of items declared by this module's root lexical scope.
    pub fn root_items(&self) -> &Items {
        &self.root_lexical_scope().items
    }

    /// The mutable collection of items declared by this module's current lexical scope.
    pub fn current_items_mut(&mut self) -> &mut Items {
        &mut self.current_lexical_scope_mut().items
    }

    pub fn current_lexical_scope_id(&self) -> LexicalScopeId {
        self.current_lexical_scope_id
    }

    /// Enters the scope with the given span in the module's lexical scope hierarchy.
    pub fn enter_lexical_scope(
        &mut self,
        handler: &Handler,
        span: Span,
    ) -> Result<LexicalScopeId, ErrorEmitted> {
        let id_opt = self.lexical_scopes_spans.get(&span);
        match id_opt {
            Some(id) => {
                let visitor_parent = self.current_lexical_scope_id;
                self.current_lexical_scope_id = *id;
                self.current_lexical_scope_mut().visitor_parent = Some(visitor_parent);

                Ok(self.current_lexical_scope_id)
            }
            None => Err(handler.emit_err(CompileError::Internal(
                "Could not find a valid lexical scope for this source location.",
                span.clone(),
            ))),
        }
    }

    /// Pushes a new scope to the module's lexical scope hierarchy.
    pub fn push_new_lexical_scope(
        &mut self,
        span: Span,
        declaration: Option<ResolvedDeclaration>,
    ) -> LexicalScopeId {
        let previous_scope_id = self.current_lexical_scope_id();
        let previous_scope = self.lexical_scopes.get(previous_scope_id).unwrap();
        let new_scoped_id = {
            self.lexical_scopes.push(LexicalScope {
                parent: Some(previous_scope_id),
                visitor_parent: Some(previous_scope_id),
                items: Items {
                    symbols_unique_while_collecting_unifications: previous_scope
                        .items
                        .symbols_unique_while_collecting_unifications
                        .clone(),
                    ..Default::default()
                },
                declaration,
                ..Default::default()
            });
            self.lexical_scopes.len() - 1
        };
        let previous_scope = self.lexical_scopes.get_mut(previous_scope_id).unwrap();
        previous_scope.children.push(new_scoped_id);
        self.current_lexical_scope_id = new_scoped_id;
        self.lexical_scopes_spans.insert(span, new_scoped_id);
        new_scoped_id
    }

    /// Pops the current scope from the module's lexical scope hierarchy.
    pub fn pop_lexical_scope(&mut self) {
        let parent_scope_id = self.current_lexical_scope().visitor_parent;
        self.current_lexical_scope_id = parent_scope_id.unwrap(); // panics if pops do not match pushes
    }

    pub fn walk_scope_chain<T>(
        &self,
        mut f: impl FnMut(&LexicalScope) -> Result<Option<T>, ErrorEmitted>,
    ) -> Result<Option<T>, ErrorEmitted> {
        let mut lexical_scope_opt = Some(self.current_lexical_scope());
        while let Some(lexical_scope) = lexical_scope_opt {
            let result = f(lexical_scope)?;
            if let Some(result) = result {
                return Ok(Some(result));
            }
            if let Some(parent_scope_id) = lexical_scope.parent {
                lexical_scope_opt = self.get_lexical_scope(parent_scope_id);
            } else {
                lexical_scope_opt = None;
            }
        }
        Ok(None)
    }

    pub fn walk_scope_chain_mut<T>(
        &mut self,
        mut f: impl FnMut(&mut LexicalScope) -> Result<Option<T>, ErrorEmitted>,
    ) -> Result<Option<T>, ErrorEmitted> {
        let mut lexical_scope_opt = Some(self.current_lexical_scope_mut());
        while let Some(lexical_scope) = lexical_scope_opt {
            let result = f(lexical_scope)?;
            if let Some(result) = result {
                return Ok(Some(result));
            }
            if let Some(parent_scope_id) = lexical_scope.parent {
                lexical_scope_opt = self.get_lexical_scope_mut(parent_scope_id);
            } else {
                lexical_scope_opt = None;
            }
        }
        Ok(None)
    }

    pub fn get_items_for_type(
        &self,
        engines: &Engines,
        type_id: TypeId,
    ) -> Vec<ResolvedTraitImplItem> {
        TraitMap::get_items_for_type(self, engines, type_id)
    }

    pub fn resolve_symbol(
        &self,
        handler: &Handler,
        engines: &Engines,
        symbol: &Ident,
    ) -> Result<(ResolvedDeclaration, ModulePathBuf), ErrorEmitted> {
        let mut last_handler = Handler::default();
        let ret = self.walk_scope_chain(|lexical_scope| {
            last_handler = Handler::default();
            Ok(lexical_scope
                .items
                .resolve_symbol(&last_handler, engines, symbol, &self.mod_path)
                .ok()
                .flatten())
        })?;

        handler.append(last_handler);

        if let Some(ret) = ret {
            Ok(ret)
        } else {
            // Symbol not found
            Err(handler.emit_err(CompileError::SymbolNotFound {
                name: symbol.clone(),
                span: symbol.span(),
            }))
        }
    }

    pub fn get_methods_for_type(
        &self,
        engines: &Engines,
        type_id: TypeId,
    ) -> Vec<ResolvedFunctionDecl> {
        self.get_items_for_type(engines, type_id)
            .into_iter()
            .filter_map(|item| match item {
                ResolvedTraitImplItem::Parsed(_) => unreachable!(),
                ResolvedTraitImplItem::Typed(item) => match item {
                    ty::TyTraitItem::Fn(decl_ref) => Some(ResolvedFunctionDecl::Typed(decl_ref)),
                    ty::TyTraitItem::Constant(_decl_ref) => None,
                    ty::TyTraitItem::Type(_decl_ref) => None,
                },
            })
            .collect::<Vec<_>>()
    }
}

/// Create a ModuleNotFound error.
/// If skip_package_name is true, then the package name is not emitted as part of the error
/// message. This is used when the module was supposed to be found in the current package rather
/// than in an external one.
pub fn module_not_found(path: &[Ident], skip_package_name: bool) -> CompileError {
    CompileError::ModuleNotFound {
        span: path
            .iter()
            .skip(if skip_package_name { 1 } else { 0 })
            .fold(path.last().unwrap().span(), |acc, this_one| {
                if acc.source_id() == this_one.span().source_id() {
                    Span::join(acc, &this_one.span())
                } else {
                    acc
                }
            }),
        name: path
            .iter()
            .skip(if skip_package_name { 1 } else { 0 })
            .map(|x| x.as_str())
            .collect::<Vec<_>>()
            .join("::"),
    }
}