aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/nameres/collector.rs
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2020-06-22 14:07:06 +0100
committerJonas Schievink <[email protected]>2020-06-24 15:53:16 +0100
commit4b03b39d5b4b00daffb120a4d2d9ea4a55a9a7ac (patch)
tree85431e53ce86bbcf16ba9b38fcc5f2ad27378722 /crates/ra_hir_def/src/nameres/collector.rs
parentb94caeb88b4aab7219d4b2f5c8c6c668199247fb (diff)
draw the rest of the owl
Diffstat (limited to 'crates/ra_hir_def/src/nameres/collector.rs')
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs134
1 files changed, 84 insertions, 50 deletions
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs
index c227b6da1..40aff830f 100644
--- a/crates/ra_hir_def/src/nameres/collector.rs
+++ b/crates/ra_hir_def/src/nameres/collector.rs
@@ -20,9 +20,7 @@ use test_utils::mark;
20use crate::{ 20use crate::{
21 attr::Attrs, 21 attr::Attrs,
22 db::DefDatabase, 22 db::DefDatabase,
23 item_tree::{ 23 item_tree::{self, ItemTree, ItemTreeId, MacroCall, Mod, ModItem, ModKind, StructDefKind},
24 FileItemTreeId, Import, ItemTree, MacroCall, Mod, ModItem, ModKind, StructDefKind,
25 },
26 nameres::{ 24 nameres::{
27 diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, 25 diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint,
28 BuiltinShadowMode, CrateDefMap, ModuleData, ModuleOrigin, ResolveMode, 26 BuiltinShadowMode, CrateDefMap, ModuleData, ModuleOrigin, ResolveMode,
@@ -106,9 +104,47 @@ impl PartialResolvedImport {
106} 104}
107 105
108#[derive(Clone, Debug, Eq, PartialEq)] 106#[derive(Clone, Debug, Eq, PartialEq)]
107struct Import {
108 pub path: ModPath,
109 pub alias: Option<ImportAlias>,
110 pub visibility: RawVisibility,
111 pub is_glob: bool,
112 pub is_prelude: bool,
113 pub is_extern_crate: bool,
114 pub is_macro_use: bool,
115}
116
117impl From<item_tree::Import> for Import {
118 fn from(it: item_tree::Import) -> Self {
119 Self {
120 path: it.path,
121 alias: it.alias,
122 visibility: it.visibility,
123 is_glob: it.is_glob,
124 is_prelude: it.is_prelude,
125 is_extern_crate: false,
126 is_macro_use: false,
127 }
128 }
129}
130
131impl From<item_tree::ExternCrate> for Import {
132 fn from(it: item_tree::ExternCrate) -> Self {
133 Self {
134 path: it.path,
135 alias: it.alias,
136 visibility: it.visibility,
137 is_glob: false,
138 is_prelude: false,
139 is_extern_crate: true,
140 is_macro_use: it.is_macro_use,
141 }
142 }
143}
144
145#[derive(Clone, Debug, Eq, PartialEq)]
109struct ImportDirective { 146struct ImportDirective {
110 module_id: LocalModuleId, 147 module_id: LocalModuleId,
111 import_id: FileItemTreeId<Import>,
112 import: Import, 148 import: Import,
113 status: PartialResolvedImport, 149 status: PartialResolvedImport,
114} 150}
@@ -297,7 +333,7 @@ impl DefCollector<'_> {
297 fn import_macros_from_extern_crate( 333 fn import_macros_from_extern_crate(
298 &mut self, 334 &mut self,
299 current_module_id: LocalModuleId, 335 current_module_id: LocalModuleId,
300 import: &Import, 336 import: &item_tree::ExternCrate,
301 ) { 337 ) {
302 log::debug!( 338 log::debug!(
303 "importing macros from extern crate: {:?} ({:?})", 339 "importing macros from extern crate: {:?} ({:?})",
@@ -703,9 +739,9 @@ impl ModCollector<'_, '_> {
703 // any other items. 739 // any other items.
704 for item in items { 740 for item in items {
705 if self.is_cfg_enabled(self.item_tree.attrs(*item)) { 741 if self.is_cfg_enabled(self.item_tree.attrs(*item)) {
706 if let ModItem::Import(import_id) = item { 742 if let ModItem::ExternCrate(id) = item {
707 let import = self.item_tree[*import_id].clone(); 743 let import = self.item_tree[*id].clone();
708 if import.is_extern_crate && import.is_macro_use { 744 if import.is_macro_use {
709 self.def_collector.import_macros_from_extern_crate(self.module_id, &import); 745 self.def_collector.import_macros_from_extern_crate(self.module_id, &import);
710 } 746 }
711 } 747 }
@@ -725,8 +761,14 @@ impl ModCollector<'_, '_> {
725 ModItem::Import(import_id) => { 761 ModItem::Import(import_id) => {
726 self.def_collector.unresolved_imports.push(ImportDirective { 762 self.def_collector.unresolved_imports.push(ImportDirective {
727 module_id: self.module_id, 763 module_id: self.module_id,
728 import_id, 764 import: self.item_tree[import_id].clone().into(),
729 import: self.item_tree[import_id].clone(), 765 status: PartialResolvedImport::Unresolved,
766 })
767 }
768 ModItem::ExternCrate(import_id) => {
769 self.def_collector.unresolved_imports.push(ImportDirective {
770 module_id: self.module_id,
771 import: self.item_tree[import_id].clone().into(),
730 status: PartialResolvedImport::Unresolved, 772 status: PartialResolvedImport::Unresolved,
731 }) 773 })
732 } 774 }
@@ -737,30 +779,28 @@ impl ModCollector<'_, '_> {
737 local_id: self.module_id, 779 local_id: self.module_id,
738 }; 780 };
739 let container = ContainerId::ModuleId(module); 781 let container = ContainerId::ModuleId(module);
740 let ast_id = self.item_tree[imp].ast_id; 782 let impl_id = ImplLoc { container, id: ItemTreeId::new(self.file_id, imp) }
741 let impl_id = 783 .intern(self.def_collector.db);
742 ImplLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
743 .intern(self.def_collector.db);
744 self.def_collector.def_map.modules[self.module_id] 784 self.def_collector.def_map.modules[self.module_id]
745 .scope 785 .scope
746 .define_impl(impl_id) 786 .define_impl(impl_id)
747 } 787 }
748 ModItem::Function(it) => { 788 ModItem::Function(id) => {
749 let it = &self.item_tree[it]; 789 let func = &self.item_tree[id];
750 def = Some(DefData { 790 def = Some(DefData {
751 id: FunctionLoc { 791 id: FunctionLoc {
752 container: container.into(), 792 container: container.into(),
753 ast_id: AstId::new(self.file_id, it.ast_id), 793 id: ItemTreeId::new(self.file_id, id),
754 } 794 }
755 .intern(self.def_collector.db) 795 .intern(self.def_collector.db)
756 .into(), 796 .into(),
757 name: &it.name, 797 name: &func.name,
758 visibility: &it.visibility, 798 visibility: &func.visibility,
759 has_constructor: false, 799 has_constructor: false,
760 }); 800 });
761 } 801 }
762 ModItem::Struct(it) => { 802 ModItem::Struct(id) => {
763 let it = &self.item_tree[it]; 803 let it = &self.item_tree[id];
764 804
765 // FIXME: check attrs to see if this is an attribute macro invocation; 805 // FIXME: check attrs to see if this is an attribute macro invocation;
766 // in which case we don't add the invocation, just a single attribute 806 // in which case we don't add the invocation, just a single attribute
@@ -768,19 +808,16 @@ impl ModCollector<'_, '_> {
768 self.collect_derives(attrs, it.ast_id.upcast()); 808 self.collect_derives(attrs, it.ast_id.upcast());
769 809
770 def = Some(DefData { 810 def = Some(DefData {
771 id: StructLoc { 811 id: StructLoc { container, id: ItemTreeId::new(self.file_id, id) }
772 container, 812 .intern(self.def_collector.db)
773 ast_id: AstId::new(self.file_id, it.ast_id), 813 .into(),
774 }
775 .intern(self.def_collector.db)
776 .into(),
777 name: &it.name, 814 name: &it.name,
778 visibility: &it.visibility, 815 visibility: &it.visibility,
779 has_constructor: it.kind != StructDefKind::Record, 816 has_constructor: it.kind != StructDefKind::Record,
780 }); 817 });
781 } 818 }
782 ModItem::Union(it) => { 819 ModItem::Union(id) => {
783 let it = &self.item_tree[it]; 820 let it = &self.item_tree[id];
784 821
785 // FIXME: check attrs to see if this is an attribute macro invocation; 822 // FIXME: check attrs to see if this is an attribute macro invocation;
786 // in which case we don't add the invocation, just a single attribute 823 // in which case we don't add the invocation, just a single attribute
@@ -788,7 +825,7 @@ impl ModCollector<'_, '_> {
788 self.collect_derives(attrs, it.ast_id.upcast()); 825 self.collect_derives(attrs, it.ast_id.upcast());
789 826
790 def = Some(DefData { 827 def = Some(DefData {
791 id: UnionLoc { container, ast_id: AstId::new(self.file_id, it.ast_id) } 828 id: UnionLoc { container, id: ItemTreeId::new(self.file_id, id) }
792 .intern(self.def_collector.db) 829 .intern(self.def_collector.db)
793 .into(), 830 .into(),
794 name: &it.name, 831 name: &it.name,
@@ -796,8 +833,8 @@ impl ModCollector<'_, '_> {
796 has_constructor: false, 833 has_constructor: false,
797 }); 834 });
798 } 835 }
799 ModItem::Enum(it) => { 836 ModItem::Enum(id) => {
800 let it = &self.item_tree[it]; 837 let it = &self.item_tree[id];
801 838
802 // FIXME: check attrs to see if this is an attribute macro invocation; 839 // FIXME: check attrs to see if this is an attribute macro invocation;
803 // in which case we don't add the invocation, just a single attribute 840 // in which case we don't add the invocation, just a single attribute
@@ -805,7 +842,7 @@ impl ModCollector<'_, '_> {
805 self.collect_derives(attrs, it.ast_id.upcast()); 842 self.collect_derives(attrs, it.ast_id.upcast());
806 843
807 def = Some(DefData { 844 def = Some(DefData {
808 id: EnumLoc { container, ast_id: AstId::new(self.file_id, it.ast_id) } 845 id: EnumLoc { container, id: ItemTreeId::new(self.file_id, id) }
809 .intern(self.def_collector.db) 846 .intern(self.def_collector.db)
810 .into(), 847 .into(),
811 name: &it.name, 848 name: &it.name,
@@ -813,14 +850,14 @@ impl ModCollector<'_, '_> {
813 has_constructor: false, 850 has_constructor: false,
814 }); 851 });
815 } 852 }
816 ModItem::Const(it) => { 853 ModItem::Const(id) => {
817 let it = &self.item_tree[it]; 854 let it = &self.item_tree[id];
818 855
819 if let Some(name) = &it.name { 856 if let Some(name) = &it.name {
820 def = Some(DefData { 857 def = Some(DefData {
821 id: ConstLoc { 858 id: ConstLoc {
822 container: container.into(), 859 container: container.into(),
823 ast_id: AstId::new(self.file_id, it.ast_id), 860 id: ItemTreeId::new(self.file_id, id),
824 } 861 }
825 .intern(self.def_collector.db) 862 .intern(self.def_collector.db)
826 .into(), 863 .into(),
@@ -830,26 +867,23 @@ impl ModCollector<'_, '_> {
830 }); 867 });
831 } 868 }
832 } 869 }
833 ModItem::Static(it) => { 870 ModItem::Static(id) => {
834 let it = &self.item_tree[it]; 871 let it = &self.item_tree[id];
835 872
836 def = Some(DefData { 873 def = Some(DefData {
837 id: StaticLoc { 874 id: StaticLoc { container, id: ItemTreeId::new(self.file_id, id) }
838 container, 875 .intern(self.def_collector.db)
839 ast_id: AstId::new(self.file_id, it.ast_id), 876 .into(),
840 }
841 .intern(self.def_collector.db)
842 .into(),
843 name: &it.name, 877 name: &it.name,
844 visibility: &it.visibility, 878 visibility: &it.visibility,
845 has_constructor: false, 879 has_constructor: false,
846 }); 880 });
847 } 881 }
848 ModItem::Trait(it) => { 882 ModItem::Trait(id) => {
849 let it = &self.item_tree[it]; 883 let it = &self.item_tree[id];
850 884
851 def = Some(DefData { 885 def = Some(DefData {
852 id: TraitLoc { container, ast_id: AstId::new(self.file_id, it.ast_id) } 886 id: TraitLoc { container, id: ItemTreeId::new(self.file_id, id) }
853 .intern(self.def_collector.db) 887 .intern(self.def_collector.db)
854 .into(), 888 .into(),
855 name: &it.name, 889 name: &it.name,
@@ -857,13 +891,13 @@ impl ModCollector<'_, '_> {
857 has_constructor: false, 891 has_constructor: false,
858 }); 892 });
859 } 893 }
860 ModItem::TypeAlias(it) => { 894 ModItem::TypeAlias(id) => {
861 let it = &self.item_tree[it]; 895 let it = &self.item_tree[id];
862 896
863 def = Some(DefData { 897 def = Some(DefData {
864 id: TypeAliasLoc { 898 id: TypeAliasLoc {
865 container: container.into(), 899 container: container.into(),
866 ast_id: AstId::new(self.file_id, it.ast_id), 900 id: ItemTreeId::new(self.file_id, id),
867 } 901 }
868 .intern(self.def_collector.db) 902 .intern(self.def_collector.db)
869 .into(), 903 .into(),