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
use crate::{
language::{ty, CallPath, Visibility},
Engines, Ident, TypeId,
};
use super::{module::Module, root::Root, submodule_namespace::SubmoduleNamespace, Path, PathBuf};
use sway_error::handler::{ErrorEmitted, Handler};
use sway_types::span::Span;
/// Enum used to pass a value asking for insertion of type into trait map when an implementation
/// of the trait cannot be found.
#[derive(Debug)]
pub enum TryInsertingTraitImplOnFailure {
Yes,
No,
}
/// The set of items that represent the namespace context passed throughout type checking.
#[derive(Clone, Debug)]
pub struct Namespace {
/// An immutable namespace that consists of the names that should always be present, no matter
/// what module or scope we are currently checking.
///
/// These include external library dependencies and (when it's added) the `std` prelude.
///
/// This is passed through type-checking in order to initialise the namespace of each submodule
/// within the project.
init: Module,
/// 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 current module being checked.
///
/// E.g. when type-checking the root module, this is equal to `[]`. When type-checking a
/// submodule of the root called "foo", this would be equal to `[foo]`.
pub(crate) mod_path: PathBuf,
}
impl Namespace {
/// Initialise the namespace at its root from the given initial namespace.
pub fn init_root(init: Module) -> Self {
let root = Root::from(init.clone());
let mod_path = vec![];
Self {
init,
root,
mod_path,
}
}
/// A reference to the path of the module currently being type-checked.
pub fn mod_path(&self) -> &Path {
&self.mod_path
}
/// Find the module that these prefixes point to
pub fn find_module_path<'a>(
&'a self,
prefixes: impl IntoIterator<Item = &'a Ident>,
) -> PathBuf {
self.mod_path.iter().chain(prefixes).cloned().collect()
}
/// A reference to the root of the project namespace.
pub fn root(&self) -> &Root {
&self.root
}
/// A mutable reference to the root of the project namespace.
pub fn root_mut(&mut self) -> &mut Root {
&mut self.root
}
/// Access to the current [Module], i.e. the module at the inner `mod_path`.
///
/// Note that the [Namespace] will automatically dereference to this [Module] when attempting
/// to call any [Module] methods.
pub fn module(&self) -> &Module {
&self.root.module[&self.mod_path]
}
/// Mutable access to the current [Module], i.e. the module at the inner `mod_path`.
///
/// Note that the [Namespace] will automatically dereference to this [Module] when attempting
/// to call any [Module] methods.
pub fn module_mut(&mut self) -> &mut Module {
&mut self.root.module[&self.mod_path]
}
/// Short-hand for calling [Root::resolve_symbol] on `root` with the `mod_path`.
pub(crate) fn resolve_symbol(
&self,
handler: &Handler,
engines: &Engines,
symbol: &Ident,
self_type: Option<TypeId>,
) -> Result<ty::TyDecl, ErrorEmitted> {
self.root
.resolve_symbol(handler, engines, &self.mod_path, symbol, self_type)
}
/// Short-hand for calling [Root::resolve_call_path] on `root` with the `mod_path`.
pub(crate) fn resolve_call_path(
&self,
handler: &Handler,
engines: &Engines,
call_path: &CallPath,
self_type: Option<TypeId>,
) -> Result<ty::TyDecl, ErrorEmitted> {
self.root
.resolve_call_path(handler, engines, &self.mod_path, call_path, self_type)
}
/// "Enter" the submodule at the given path by returning a new [SubmoduleNamespace].
///
/// Here we temporarily change `mod_path` to the given `dep_mod_path` and wrap `self` in a
/// [SubmoduleNamespace] type. When dropped, the [SubmoduleNamespace] resets the `mod_path`
/// back to the original path so that we can continue type-checking the current module after
/// finishing with the dependency.
pub(crate) fn enter_submodule(
&mut self,
mod_name: Ident,
visibility: Visibility,
module_span: Span,
) -> SubmoduleNamespace {
let init = self.init.clone();
self.submodules.entry(mod_name.to_string()).or_insert(init);
let submod_path: Vec<_> = self
.mod_path
.iter()
.cloned()
.chain(Some(mod_name.clone()))
.collect();
let parent_mod_path = std::mem::replace(&mut self.mod_path, submod_path);
self.name = Some(mod_name);
self.span = Some(module_span);
self.visibility = visibility;
self.is_external = false;
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()
}
}