1 //! Describes items defined or visible (ie, imported) in a certain scope.
2 //! This is shared between modules and blocks.
4 use hir_expand::name::Name;
5 use once_cell::sync::Lazy;
6 use rustc_hash::FxHashMap;
8 use crate::{per_ns::PerNs, BuiltinType, ImplId, LocalImportId, MacroDefId, ModuleDefId, TraitId};
10 #[derive(Debug, Default, PartialEq, Eq)]
11 pub struct ItemScope {
12 items: FxHashMap<Name, Resolution>,
14 /// Macros visible in current module in legacy textual scope
16 /// For macros invoked by an unqualified identifier like `bar!()`, `legacy_macros` will be searched in first.
17 /// If it yields no result, then it turns to module scoped `macros`.
18 /// It macros with name qualified with a path like `crate::foo::bar!()`, `legacy_macros` will be skipped,
19 /// and only normal scoped `macros` will be searched in.
21 /// Note that this automatically inherit macros defined textually before the definition of module itself.
23 /// Module scoped macros will be inserted into `items` instead of here.
24 // FIXME: Macro shadowing in one module is not properly handled. Non-item place macros will
25 // be all resolved to the last one defined if shadowing happens.
26 legacy_macros: FxHashMap<Name, MacroDefId>,
29 static BUILTIN_SCOPE: Lazy<FxHashMap<Name, Resolution>> = Lazy::new(|| {
33 (name.clone(), Resolution { def: PerNs::types(ty.clone().into()), import: None })
38 /// Shadow mode for builtin type which can be shadowed by module.
39 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
40 pub(crate) enum BuiltinShadowMode {
47 /// Legacy macros can only be accessed through special methods like `get_legacy_macros`.
48 /// Other methods will only resolve values, types and module scoped macros only.
50 pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a Name, &'a Resolution)> + 'a {
52 self.items.iter().chain(BUILTIN_SCOPE.iter())
55 pub fn declarations(&self) -> impl Iterator<Item = ModuleDefId> + '_ {
57 .filter_map(|(_name, res)| if res.import.is_none() { Some(res.def) } else { None })
59 per_ns.take_types().into_iter().chain(per_ns.take_values().into_iter())
63 pub fn impls(&self) -> impl Iterator<Item = ImplId> + ExactSizeIterator + '_ {
64 self.impls.iter().copied()
67 /// Iterate over all module scoped macros
68 pub(crate) fn macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a {
71 .filter_map(|(name, res)| res.def.take_macros().map(|macro_| (name, macro_)))
74 /// Iterate over all legacy textual scoped macros visible at the end of the module
75 pub(crate) fn legacy_macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a {
76 self.legacy_macros.iter().map(|(name, def)| (name, *def))
79 /// Get a name from current module scope, legacy macros are not included
80 pub(crate) fn get(&self, name: &Name, shadow: BuiltinShadowMode) -> Option<&Resolution> {
82 BuiltinShadowMode::Module => self.items.get(name).or_else(|| BUILTIN_SCOPE.get(name)),
83 BuiltinShadowMode::Other => {
84 let item = self.items.get(name);
85 if let Some(res) = item {
86 if let Some(ModuleDefId::ModuleId(_)) = res.def.take_types() {
87 return BUILTIN_SCOPE.get(name).or(item);
91 item.or_else(|| BUILTIN_SCOPE.get(name))
96 pub(crate) fn traits<'a>(&'a self) -> impl Iterator<Item = TraitId> + 'a {
97 self.items.values().filter_map(|r| match r.def.take_types() {
98 Some(ModuleDefId::TraitId(t)) => Some(t),
103 pub(crate) fn get_legacy_macro(&self, name: &Name) -> Option<MacroDefId> {
104 self.legacy_macros.get(name).copied()
107 pub(crate) fn define_impl(&mut self, imp: ImplId) {
111 pub(crate) fn define_legacy_macro(&mut self, name: Name, mac: MacroDefId) {
112 self.legacy_macros.insert(name, mac);
115 pub(crate) fn push_res(
119 import: Option<LocalImportId>,
121 let mut changed = false;
122 let existing = self.items.entry(name.clone()).or_default();
124 if existing.def.types.is_none() && res.def.types.is_some() {
125 existing.def.types = res.def.types;
126 existing.import = import.or(res.import);
129 if existing.def.values.is_none() && res.def.values.is_some() {
130 existing.def.values = res.def.values;
131 existing.import = import.or(res.import);
134 if existing.def.macros.is_none() && res.def.macros.is_some() {
135 existing.def.macros = res.def.macros;
136 existing.import = import.or(res.import);
140 if existing.def.is_none()
142 && existing.import.is_none()
143 && res.import.is_some()
145 existing.import = res.import;
150 pub(crate) fn collect_resolutions(&self) -> Vec<(Name, Resolution)> {
151 self.items.iter().map(|(name, res)| (name.clone(), res.clone())).collect()
154 pub(crate) fn collect_legacy_macros(&self) -> FxHashMap<Name, MacroDefId> {
155 self.legacy_macros.clone()
159 #[derive(Debug, Clone, PartialEq, Eq, Default)]
160 pub struct Resolution {
161 /// None for unresolved
163 /// ident by which this is imported into local scope.
164 pub import: Option<LocalImportId>,