2019-11-24 12:00:50 -06:00
|
|
|
//! In rust, it is possible to have a value, a type and a macro with the same
|
|
|
|
//! name without conflicts.
|
|
|
|
//!
|
|
|
|
//! `PerNs` (per namespace) captures this.
|
2019-09-30 03:58:53 -05:00
|
|
|
|
2023-08-16 04:18:11 -05:00
|
|
|
use crate::{
|
|
|
|
item_scope::{ImportId, ImportOrExternCrate, ItemInNs},
|
|
|
|
visibility::Visibility,
|
|
|
|
MacroId, ModuleDefId,
|
|
|
|
};
|
2019-09-09 07:54:02 -05:00
|
|
|
|
2020-12-31 14:41:49 -06:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
2019-09-13 08:38:59 -05:00
|
|
|
pub struct PerNs {
|
2023-08-16 04:18:11 -05:00
|
|
|
pub types: Option<(ModuleDefId, Visibility, Option<ImportOrExternCrate>)>,
|
|
|
|
pub values: Option<(ModuleDefId, Visibility, Option<ImportId>)>,
|
|
|
|
pub macros: Option<(MacroId, Visibility, Option<ImportId>)>,
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
2019-09-13 08:38:59 -05:00
|
|
|
impl Default for PerNs {
|
2019-03-16 09:17:50 -05:00
|
|
|
fn default() -> Self {
|
2019-09-09 07:54:02 -05:00
|
|
|
PerNs { types: None, values: None, macros: None }
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-13 08:38:59 -05:00
|
|
|
impl PerNs {
|
|
|
|
pub fn none() -> PerNs {
|
2019-09-09 07:54:02 -05:00
|
|
|
PerNs { types: None, values: None, macros: None }
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
2023-08-17 03:52:13 -05:00
|
|
|
pub fn values(t: ModuleDefId, v: Visibility, i: Option<ImportId>) -> PerNs {
|
|
|
|
PerNs { types: None, values: Some((t, v, i)), macros: None }
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
2023-08-17 03:52:13 -05:00
|
|
|
pub fn types(t: ModuleDefId, v: Visibility, i: Option<ImportOrExternCrate>) -> PerNs {
|
|
|
|
PerNs { types: Some((t, v, i)), values: None, macros: None }
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
2023-08-17 03:52:13 -05:00
|
|
|
pub fn both(
|
|
|
|
types: ModuleDefId,
|
|
|
|
values: ModuleDefId,
|
|
|
|
v: Visibility,
|
|
|
|
i: Option<ImportOrExternCrate>,
|
|
|
|
) -> PerNs {
|
|
|
|
PerNs {
|
|
|
|
types: Some((types, v, i)),
|
|
|
|
values: Some((values, v, i.and_then(ImportOrExternCrate::into_import))),
|
|
|
|
macros: None,
|
|
|
|
}
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
2023-08-17 03:52:13 -05:00
|
|
|
pub fn macros(macro_: MacroId, v: Visibility, i: Option<ImportId>) -> PerNs {
|
|
|
|
PerNs { types: None, values: None, macros: Some((macro_, v, i)) }
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 07:54:02 -05:00
|
|
|
pub fn is_none(&self) -> bool {
|
|
|
|
self.types.is_none() && self.values.is_none() && self.macros.is_none()
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
2022-08-15 09:41:51 -05:00
|
|
|
pub fn is_full(&self) -> bool {
|
|
|
|
self.types.is_some() && self.values.is_some() && self.macros.is_some()
|
|
|
|
}
|
|
|
|
|
2019-10-31 10:45:10 -05:00
|
|
|
pub fn take_types(self) -> Option<ModuleDefId> {
|
2019-12-25 08:00:10 -06:00
|
|
|
self.types.map(|it| it.0)
|
|
|
|
}
|
|
|
|
|
2023-08-17 03:52:13 -05:00
|
|
|
pub fn take_types_full(self) -> Option<(ModuleDefId, Visibility, Option<ImportOrExternCrate>)> {
|
|
|
|
self.types
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
2019-10-31 10:45:10 -05:00
|
|
|
pub fn take_values(self) -> Option<ModuleDefId> {
|
2019-12-25 08:00:10 -06:00
|
|
|
self.values.map(|it| it.0)
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
2023-08-17 03:52:13 -05:00
|
|
|
pub fn take_values_import(self) -> Option<(ModuleDefId, Option<ImportId>)> {
|
|
|
|
self.values.map(|it| (it.0, it.2))
|
|
|
|
}
|
|
|
|
|
2022-03-08 14:41:19 -06:00
|
|
|
pub fn take_macros(self) -> Option<MacroId> {
|
2019-12-25 08:00:10 -06:00
|
|
|
self.macros.map(|it| it.0)
|
|
|
|
}
|
|
|
|
|
2023-08-17 03:52:13 -05:00
|
|
|
pub fn take_macros_import(self) -> Option<(MacroId, Option<ImportId>)> {
|
|
|
|
self.macros.map(|it| (it.0, it.2))
|
|
|
|
}
|
|
|
|
|
2019-12-26 09:00:10 -06:00
|
|
|
pub fn filter_visibility(self, mut f: impl FnMut(Visibility) -> bool) -> PerNs {
|
2021-06-10 15:03:16 -05:00
|
|
|
let _p = profile::span("PerNs::filter_visibility");
|
2019-12-25 11:05:16 -06:00
|
|
|
PerNs {
|
2023-08-16 04:18:11 -05:00
|
|
|
types: self.types.filter(|&(_, v, _)| f(v)),
|
|
|
|
values: self.values.filter(|&(_, v, _)| f(v)),
|
|
|
|
macros: self.macros.filter(|&(_, v, _)| f(v)),
|
2019-12-25 11:05:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-26 09:00:10 -06:00
|
|
|
pub fn with_visibility(self, vis: Visibility) -> PerNs {
|
2019-12-25 08:00:10 -06:00
|
|
|
PerNs {
|
2023-08-16 04:18:11 -05:00
|
|
|
types: self.types.map(|(it, _, c)| (it, vis, c)),
|
|
|
|
values: self.values.map(|(it, _, c)| (it, vis, c)),
|
|
|
|
macros: self.macros.map(|(it, _, import)| (it, vis, import)),
|
2019-12-25 08:00:10 -06:00
|
|
|
}
|
2019-09-09 07:54:02 -05:00
|
|
|
}
|
|
|
|
|
2019-09-13 08:38:59 -05:00
|
|
|
pub fn or(self, other: PerNs) -> PerNs {
|
2019-09-09 07:54:02 -05:00
|
|
|
PerNs {
|
|
|
|
types: self.types.or(other.types),
|
|
|
|
values: self.values.or(other.values),
|
|
|
|
macros: self.macros.or(other.macros),
|
|
|
|
}
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
2020-05-20 16:51:20 -05:00
|
|
|
|
2022-08-15 09:41:51 -05:00
|
|
|
pub fn or_else(self, f: impl FnOnce() -> PerNs) -> PerNs {
|
|
|
|
if self.is_full() {
|
|
|
|
self
|
|
|
|
} else {
|
|
|
|
self.or(f())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 05:17:41 -05:00
|
|
|
pub fn iter_items(self) -> impl Iterator<Item = (ItemInNs, Option<ImportOrExternCrate>)> {
|
2021-06-10 15:03:16 -05:00
|
|
|
let _p = profile::span("PerNs::iter_items");
|
2020-05-20 16:51:20 -05:00
|
|
|
self.types
|
2023-08-17 05:17:41 -05:00
|
|
|
.map(|it| (ItemInNs::Types(it.0), it.2))
|
2020-05-20 16:51:20 -05:00
|
|
|
.into_iter()
|
2023-08-17 05:17:41 -05:00
|
|
|
.chain(
|
|
|
|
self.values
|
|
|
|
.map(|it| (ItemInNs::Values(it.0), it.2.map(ImportOrExternCrate::Import)))
|
|
|
|
.into_iter(),
|
|
|
|
)
|
|
|
|
.chain(
|
|
|
|
self.macros
|
|
|
|
.map(|it| (ItemInNs::Macros(it.0), it.2.map(ImportOrExternCrate::Import)))
|
|
|
|
.into_iter(),
|
|
|
|
)
|
2020-05-20 16:51:20 -05:00
|
|
|
}
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|