sway_core/semantic_analysis/namespace/
namespace.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
412
413
414
415
416
use crate::{language::Visibility, Engines, Ident};

use super::{module::Module, root::Root, ModulePath, ModulePathBuf};

use sway_error::handler::{ErrorEmitted, Handler};
use sway_types::{
    constants::{CONTRACT_ID, CORE, PRELUDE, STD},
    span::Span,
};

/// The set of items that represent the namespace context passed throughout type checking.
#[derive(Clone, Debug)]
pub struct Namespace {
    /// The `root` of the project namespace.
    ///
    /// From the root, the entirety of the project's namespace can always be accessed.
    ///
    /// The root is initialised from the `init` namespace before type-checking begins.
    pub(crate) root: Root,
    /// 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]`.
    pub(crate) current_mod_path: ModulePathBuf,
}

impl Namespace {
    /// Initialize the namespace
    /// See also the factory functions in contract_helpers.rs
    ///
    /// If `import_preludes_into_root` is true then core::prelude::* and std::prelude::* will be
    /// imported into the root module, provided core and std are available in the external modules.
    pub fn new(
        handler: &Handler,
        engines: &Engines,
        package_root: Root,
        import_preludes_into_root: bool,
    ) -> Result<Self, ErrorEmitted> {
        let package_name = package_root.current_package_name().clone();
        let mut res = Self {
            root: package_root,
            current_mod_path: vec![package_name],
        };

        if import_preludes_into_root {
            res.import_implicits(handler, engines)?;
        }
        Ok(res)
    }

    pub fn root(self) -> Root {
        self.root
    }

    pub fn root_ref(&self) -> &Root {
        &self.root
    }

    pub fn current_module(&self) -> &Module {
        self.root
            .module_in_current_package(&self.current_mod_path)
            .unwrap_or_else(|| panic!("Could not retrieve submodule for mod_path."))
    }

    pub fn current_module_mut(&mut self) -> &mut Module {
        self.root
            .module_mut_in_current_package(&self.current_mod_path)
            .unwrap_or_else(|| panic!("Could not retrieve submodule for mod_path."))
    }

    pub(crate) fn current_module_has_submodule(&self, submod_name: &Ident) -> bool {
        self.current_module()
            .submodule(&[submod_name.clone()])
            .is_some()
    }

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

    /// A reference to the path of the module currently being processed.
    pub fn current_mod_path(&self) -> &ModulePathBuf {
        &self.current_mod_path
    }

    /// Prepends the module path into the prefixes.
    pub fn prepend_module_path<'a>(
        &'a self,
        prefixes: impl IntoIterator<Item = &'a Ident>,
    ) -> ModulePathBuf {
        self.current_mod_path
            .iter()
            .chain(prefixes)
            .cloned()
            .collect()
    }

    /// Convert a parsed path to a full path.
    pub fn parsed_path_to_full_path(
        &self,
        _engines: &Engines,
        parsed_path: &ModulePathBuf,
        is_relative_to_package_root: bool,
    ) -> ModulePathBuf {
        if is_relative_to_package_root {
            // Path is relative to the root module in the current package. Prepend the package name
            let mut path = vec![self.current_package_name().clone()];
            for ident in parsed_path.iter() {
                path.push(ident.clone())
            }
            path
        } else if self.current_module_has_submodule(&parsed_path[0]) {
            // The first identifier is a submodule of the current module
            // The path is therefore assumed to be relative to the current module, so prepend the current module path.
            self.prepend_module_path(parsed_path)
        } else if self.module_is_external(parsed_path) {
            // The path refers to an external module, so the path is already a full path.
            parsed_path.to_vec()
        } else {
            // The first identifier is neither a submodule nor an external package. It must
            // therefore refer to a binding in the local environment
            self.prepend_module_path(parsed_path)
        }
    }

    pub fn current_package_root_module(&self) -> &Module {
        self.root.current_package_root_module()
    }

    pub fn module_from_absolute_path(&self, path: &ModulePathBuf) -> Option<&Module> {
        self.root.module_from_absolute_path(path)
    }

    // Like module_from_absolute_path, but throws an error if the module is not found
    pub fn require_module_from_absolute_path(
        &self,
        handler: &Handler,
        path: &ModulePathBuf,
    ) -> Result<&Module, ErrorEmitted> {
        self.root.require_module(handler, path)
    }

    /// Returns true if the current module being checked is a direct or indirect submodule of
    /// the module given by the `absolute_module_path`.
    ///
    /// The current module being checked is determined by `mod_path`.
    ///
    /// E.g., the `mod_path` `[fist, second, third]` of the root `foo` is a submodule of the module
    /// `[foo, first]`. Note that the `mod_path` does not contain the root name, while the
    /// `absolute_module_path` always contains it.
    ///
    /// If the current module being checked is the same as the module given by the `absolute_module_path`,
    /// the `true_if_same` is returned.
    pub(crate) fn module_is_submodule_of(
        &self,
        absolute_module_path: &ModulePath,
        true_if_same: bool,
    ) -> bool {
        if self.current_mod_path.len() < absolute_module_path.len() {
            return false;
        }

        let is_submodule = absolute_module_path
            .iter()
            .zip(self.current_mod_path.iter())
            .all(|(left, right)| left == right);

        if is_submodule {
            if self.current_mod_path.len() == absolute_module_path.len() {
                true_if_same
            } else {
                true
            }
        } else {
            false
        }
    }

    /// Returns true if the module given by the `absolute_module_path` is external
    /// to the current package. External modules are imported in the `Forc.toml` file.
    pub(crate) fn module_is_external(&self, absolute_module_path: &ModulePath) -> bool {
        assert!(!absolute_module_path.is_empty(), "Absolute module path must have at least one element, because it always contains the package name.");

        self.root.current_package_name() != &absolute_module_path[0]
    }

    pub fn package_exists(&self, name: &Ident) -> bool {
        self.module_from_absolute_path(&vec![name.clone()])
            .is_some()
    }

    pub(crate) fn module_has_binding(
        &self,
        engines: &Engines,
        mod_path: &ModulePathBuf,
        symbol: &Ident,
    ) -> bool {
        let dummy_handler = Handler::default();
        if let Some(module) = self.module_from_absolute_path(mod_path) {
            module
                .resolve_symbol(&dummy_handler, engines, symbol)
                .is_ok()
        } else {
            false
        }
    }

    // Import core::prelude::*, std::prelude::* and ::CONTRACT_ID as appropriate into the current module
    fn import_implicits(
        &mut self,
        handler: &Handler,
        engines: &Engines,
    ) -> Result<(), ErrorEmitted> {
        // Import preludes
        let package_name = self.current_package_name().to_string();
        let core_string = CORE.to_string();
        let core_ident = Ident::new_no_span(core_string.clone());
        let prelude_ident = Ident::new_no_span(PRELUDE.to_string());
        if package_name == CORE {
            // Do nothing
        } else if package_name == STD {
            // Import core::prelude::*
            assert!(self.root.exists_as_external(&core_string));
            self.root.star_import(
                handler,
                engines,
                &[core_ident, prelude_ident],
                &self.current_mod_path,
                Visibility::Private,
            )?
        } else {
            // Import core::prelude::* and std::prelude::*
            if self.root.exists_as_external(&core_string) {
                self.root.star_import(
                    handler,
                    engines,
                    &[core_ident, prelude_ident.clone()],
                    &self.current_mod_path,
                    Visibility::Private,
                )?;
            }

            let std_string = STD.to_string();
            // Only import std::prelude::* if std exists as a dependency
            if self.root.exists_as_external(&std_string) {
                self.root.star_import(
                    handler,
                    engines,
                    &[Ident::new_no_span(std_string), prelude_ident],
                    &self.current_mod_path,
                    Visibility::Private,
                )?
            }
        }

        // Import contract id. CONTRACT_ID is declared in the root module, so only import it into non-root modules
        if self.root.is_contract_package() && self.current_mod_path.len() > 1 {
            // import ::CONTRACT_ID
            self.root.item_import(
                handler,
                engines,
                &[Ident::new_no_span(package_name)],
                &Ident::new_no_span(CONTRACT_ID.to_string()),
                &self.current_mod_path,
                None,
                Visibility::Private,
            )?
        }

        Ok(())
    }

    pub(crate) fn enter_submodule(
        &mut self,
        handler: &Handler,
        engines: &Engines,
        mod_name: Ident,
        visibility: Visibility,
        module_span: Span,
    ) -> Result<(), ErrorEmitted> {
        let mut import_implicits = false;

        // Ensure the new module exists and is initialized properly
        if !self
            .current_module()
            .submodules()
            .contains_key(&mod_name.to_string())
        {
            // Entering a new module. Add a new one.
            self.current_module_mut()
                .add_new_submodule(&mod_name, visibility, Some(module_span));
            import_implicits = true;
        }

        // Update self to point to the new module
        self.current_mod_path.push(mod_name.clone());

        // Import implicits into the newly created module.
        if import_implicits {
            self.import_implicits(handler, engines)?;
        }

        Ok(())
    }

    /// Pushes a new submodule to the namespace's module hierarchy.
    pub fn push_submodule(
        &mut self,
        handler: &Handler,
        engines: &Engines,
        mod_name: Ident,
        visibility: Visibility,
        module_span: Span,
    ) -> Result<(), ErrorEmitted> {
        match self.enter_submodule(handler, engines, mod_name, visibility, module_span) {
            Ok(_) => Ok(()),
            Err(e) => Err(e),
        }
    }

    /// Pops the current submodule from the namespace's module hierarchy.
    pub fn pop_submodule(&mut self) {
        self.current_mod_path.pop();
    }

    pub(crate) fn star_import_to_current_module(
        &mut self,
        handler: &Handler,
        engines: &Engines,
        src: &ModulePath,
        visibility: Visibility,
    ) -> Result<(), ErrorEmitted> {
        self.root
            .star_import(handler, engines, src, &self.current_mod_path, visibility)
    }

    pub(crate) fn variant_star_import_to_current_module(
        &mut self,
        handler: &Handler,
        engines: &Engines,
        src: &ModulePath,
        enum_name: &Ident,
        visibility: Visibility,
    ) -> Result<(), ErrorEmitted> {
        self.root.variant_star_import(
            handler,
            engines,
            src,
            &self.current_mod_path,
            enum_name,
            visibility,
        )
    }

    pub(crate) fn self_import_to_current_module(
        &mut self,
        handler: &Handler,
        engines: &Engines,
        src: &ModulePath,
        alias: Option<Ident>,
        visibility: Visibility,
    ) -> Result<(), ErrorEmitted> {
        self.root.self_import(
            handler,
            engines,
            src,
            &self.current_mod_path,
            alias,
            visibility,
        )
    }

    pub(crate) fn item_import_to_current_module(
        &mut self,
        handler: &Handler,
        engines: &Engines,
        src: &ModulePath,
        item: &Ident,
        alias: Option<Ident>,
        visibility: Visibility,
    ) -> Result<(), ErrorEmitted> {
        self.root.item_import(
            handler,
            engines,
            src,
            item,
            &self.current_mod_path,
            alias,
            visibility,
        )
    }

    #[allow(clippy::too_many_arguments)]
    pub(crate) fn variant_import_to_current_module(
        &mut self,
        handler: &Handler,
        engines: &Engines,
        src: &ModulePath,
        enum_name: &Ident,
        variant_name: &Ident,
        alias: Option<Ident>,
        visibility: Visibility,
    ) -> Result<(), ErrorEmitted> {
        self.root.variant_import(
            handler,
            engines,
            src,
            enum_name,
            variant_name,
            &self.current_mod_path,
            alias,
            visibility,
        )
    }
}