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
|
|
|
|
2019-10-31 10:45:10 -05:00
|
|
|
use hir_expand::MacroDefId;
|
|
|
|
|
|
|
|
use crate::ModuleDefId;
|
2019-09-09 07:54:02 -05:00
|
|
|
|
2019-03-16 09:17:50 -05:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
2019-09-13 08:38:59 -05:00
|
|
|
pub struct PerNs {
|
2019-10-31 10:45:10 -05:00
|
|
|
pub types: Option<ModuleDefId>,
|
|
|
|
pub values: Option<ModuleDefId>,
|
|
|
|
pub macros: Option<MacroDefId>,
|
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
|
|
|
}
|
|
|
|
|
2019-10-31 10:45:10 -05:00
|
|
|
pub fn values(t: ModuleDefId) -> PerNs {
|
2019-09-09 07:54:02 -05:00
|
|
|
PerNs { types: None, values: Some(t), macros: None }
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
2019-10-31 10:45:10 -05:00
|
|
|
pub fn types(t: ModuleDefId) -> PerNs {
|
2019-09-09 07:54:02 -05:00
|
|
|
PerNs { types: Some(t), values: None, macros: None }
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
2019-10-31 10:45:10 -05:00
|
|
|
pub fn both(types: ModuleDefId, values: ModuleDefId) -> PerNs {
|
2019-09-09 07:54:02 -05:00
|
|
|
PerNs { types: Some(types), values: Some(values), macros: None }
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
2019-10-31 10:45:10 -05:00
|
|
|
pub fn macros(macro_: MacroDefId) -> PerNs {
|
2019-09-09 07:54:02 -05:00
|
|
|
PerNs { types: None, values: None, macros: Some(macro_) }
|
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
|
|
|
}
|
|
|
|
|
2019-10-31 10:45:10 -05:00
|
|
|
pub fn take_types(self) -> Option<ModuleDefId> {
|
2019-09-09 07:54:02 -05:00
|
|
|
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-09-09 07:54:02 -05:00
|
|
|
self.values
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
2019-11-24 08:00:10 -06:00
|
|
|
pub fn take_macros(self) -> Option<MacroDefId> {
|
2019-09-09 07:54:02 -05:00
|
|
|
self.macros
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|