diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-26 11:19:38 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-26 11:19:38 +0000 |
commit | 8254244e4970b085809d42a34282649b4a4e16a2 (patch) | |
tree | 380b2c4835bd7ad75a66d26cab8e25e0be801182 /crates/ra_hir/src/nameres/raw.rs | |
parent | 71f8619b4e614aa9c61be6b2851498050e7f63ae (diff) | |
parent | e231277ab2f1ebb3040457e89b92540e599dbb3e (diff) |
Merge #1055
1055: store macro def inside macro id r=matklad a=matklad
This makes macro expansion resilient to changes to code inside the macro
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/nameres/raw.rs')
-rw-r--r-- | crates/ra_hir/src/nameres/raw.rs | 80 |
1 files changed, 33 insertions, 47 deletions
diff --git a/crates/ra_hir/src/nameres/raw.rs b/crates/ra_hir/src/nameres/raw.rs index f8ba398ec..ee0719ee0 100644 --- a/crates/ra_hir/src/nameres/raw.rs +++ b/crates/ra_hir/src/nameres/raw.rs | |||
@@ -4,7 +4,6 @@ use std::{ | |||
4 | }; | 4 | }; |
5 | 5 | ||
6 | use test_utils::tested_by; | 6 | use test_utils::tested_by; |
7 | use ra_db::FileId; | ||
8 | use ra_arena::{Arena, impl_arena_id, RawId, map::ArenaMap}; | 7 | use ra_arena::{Arena, impl_arena_id, RawId, map::ArenaMap}; |
9 | use ra_syntax::{ | 8 | use ra_syntax::{ |
10 | AstNode, SourceFile, AstPtr, TreeArc, | 9 | AstNode, SourceFile, AstPtr, TreeArc, |
@@ -16,6 +15,10 @@ use crate::{ | |||
16 | ids::{SourceFileItemId, SourceFileItems}, | 15 | ids::{SourceFileItemId, SourceFileItems}, |
17 | }; | 16 | }; |
18 | 17 | ||
18 | /// `RawItems` is a set of top-level items in a file (except for impls). | ||
19 | /// | ||
20 | /// It is the input to name resolution algorithm. `RawItems` are not invalidated | ||
21 | /// on most edits. | ||
19 | #[derive(Debug, Default, PartialEq, Eq)] | 22 | #[derive(Debug, Default, PartialEq, Eq)] |
20 | pub struct RawItems { | 23 | pub struct RawItems { |
21 | modules: Arena<Module, ModuleData>, | 24 | modules: Arena<Module, ModuleData>, |
@@ -32,11 +35,11 @@ pub struct ImportSourceMap { | |||
32 | } | 35 | } |
33 | 36 | ||
34 | impl ImportSourceMap { | 37 | impl ImportSourceMap { |
35 | pub(crate) fn insert(&mut self, import: ImportId, segment: &ast::PathSegment) { | 38 | fn insert(&mut self, import: ImportId, segment: &ast::PathSegment) { |
36 | self.map.insert(import, AstPtr::new(segment)) | 39 | self.map.insert(import, AstPtr::new(segment)) |
37 | } | 40 | } |
38 | 41 | ||
39 | pub fn get(&self, source: &ModuleSource, import: ImportId) -> TreeArc<ast::PathSegment> { | 42 | pub(crate) fn get(&self, source: &ModuleSource, import: ImportId) -> TreeArc<ast::PathSegment> { |
40 | let file = match source { | 43 | let file = match source { |
41 | ModuleSource::SourceFile(file) => &*file, | 44 | ModuleSource::SourceFile(file) => &*file, |
42 | ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(), | 45 | ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(), |
@@ -47,40 +50,27 @@ impl ImportSourceMap { | |||
47 | } | 50 | } |
48 | 51 | ||
49 | impl RawItems { | 52 | impl RawItems { |
50 | pub(crate) fn raw_items_query(db: &impl DefDatabase, file_id: FileId) -> Arc<RawItems> { | 53 | pub(crate) fn raw_items_query(db: &impl DefDatabase, file_id: HirFileId) -> Arc<RawItems> { |
51 | db.raw_items_with_source_map(file_id).0 | 54 | db.raw_items_with_source_map(file_id).0 |
52 | } | 55 | } |
53 | 56 | ||
54 | pub(crate) fn raw_items_with_source_map_query( | 57 | pub(crate) fn raw_items_with_source_map_query( |
55 | db: &impl DefDatabase, | 58 | db: &impl DefDatabase, |
56 | file_id: FileId, | 59 | file_id: HirFileId, |
57 | ) -> (Arc<RawItems>, Arc<ImportSourceMap>) { | 60 | ) -> (Arc<RawItems>, Arc<ImportSourceMap>) { |
58 | let mut collector = RawItemsCollector { | 61 | let mut collector = RawItemsCollector { |
59 | raw_items: RawItems::default(), | 62 | raw_items: RawItems::default(), |
60 | source_file_items: db.file_items(file_id.into()), | 63 | source_file_items: db.file_items(file_id.into()), |
61 | source_map: ImportSourceMap::default(), | 64 | source_map: ImportSourceMap::default(), |
62 | }; | 65 | }; |
63 | let source_file = db.parse(file_id); | 66 | let source_file = db.hir_parse(file_id); |
64 | collector.process_module(None, &*source_file); | 67 | collector.process_module(None, &*source_file); |
65 | (Arc::new(collector.raw_items), Arc::new(collector.source_map)) | 68 | (Arc::new(collector.raw_items), Arc::new(collector.source_map)) |
66 | } | 69 | } |
67 | 70 | ||
68 | pub(crate) fn items(&self) -> &[RawItem] { | 71 | pub(super) fn items(&self) -> &[RawItem] { |
69 | &self.items | 72 | &self.items |
70 | } | 73 | } |
71 | |||
72 | // We can't use queries during name resolution for fear of cycles, so this | ||
73 | // is a query-less variant of the above function. | ||
74 | pub(crate) fn from_source_file(source_file: &SourceFile, file_id: HirFileId) -> RawItems { | ||
75 | let source_file_items = SourceFileItems::from_source_file(source_file, file_id); | ||
76 | let mut collector = RawItemsCollector { | ||
77 | raw_items: RawItems::default(), | ||
78 | source_file_items: Arc::new(source_file_items), | ||
79 | source_map: ImportSourceMap::default(), | ||
80 | }; | ||
81 | collector.process_module(None, &*source_file); | ||
82 | collector.raw_items | ||
83 | } | ||
84 | } | 74 | } |
85 | 75 | ||
86 | impl Index<Module> for RawItems { | 76 | impl Index<Module> for RawItems { |
@@ -112,7 +102,7 @@ impl Index<Macro> for RawItems { | |||
112 | } | 102 | } |
113 | 103 | ||
114 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | 104 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] |
115 | pub(crate) enum RawItem { | 105 | pub(super) enum RawItem { |
116 | Module(Module), | 106 | Module(Module), |
117 | Import(ImportId), | 107 | Import(ImportId), |
118 | Def(Def), | 108 | Def(Def), |
@@ -120,11 +110,11 @@ pub(crate) enum RawItem { | |||
120 | } | 110 | } |
121 | 111 | ||
122 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 112 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
123 | pub(crate) struct Module(RawId); | 113 | pub(super) struct Module(RawId); |
124 | impl_arena_id!(Module); | 114 | impl_arena_id!(Module); |
125 | 115 | ||
126 | #[derive(Debug, PartialEq, Eq)] | 116 | #[derive(Debug, PartialEq, Eq)] |
127 | pub(crate) enum ModuleData { | 117 | pub(super) enum ModuleData { |
128 | Declaration { name: Name, source_item_id: SourceFileItemId }, | 118 | Declaration { name: Name, source_item_id: SourceFileItemId }, |
129 | Definition { name: Name, source_item_id: SourceFileItemId, items: Vec<RawItem> }, | 119 | Definition { name: Name, source_item_id: SourceFileItemId, items: Vec<RawItem> }, |
130 | } | 120 | } |
@@ -135,26 +125,26 @@ impl_arena_id!(ImportId); | |||
135 | 125 | ||
136 | #[derive(Debug, Clone, PartialEq, Eq)] | 126 | #[derive(Debug, Clone, PartialEq, Eq)] |
137 | pub struct ImportData { | 127 | pub struct ImportData { |
138 | pub(crate) path: Path, | 128 | pub(super) path: Path, |
139 | pub(crate) alias: Option<Name>, | 129 | pub(super) alias: Option<Name>, |
140 | pub(crate) is_glob: bool, | 130 | pub(super) is_glob: bool, |
141 | pub(crate) is_prelude: bool, | 131 | pub(super) is_prelude: bool, |
142 | pub(crate) is_extern_crate: bool, | 132 | pub(super) is_extern_crate: bool, |
143 | } | 133 | } |
144 | 134 | ||
145 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 135 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
146 | pub(crate) struct Def(RawId); | 136 | pub(super) struct Def(RawId); |
147 | impl_arena_id!(Def); | 137 | impl_arena_id!(Def); |
148 | 138 | ||
149 | #[derive(Debug, PartialEq, Eq)] | 139 | #[derive(Debug, PartialEq, Eq)] |
150 | pub(crate) struct DefData { | 140 | pub(super) struct DefData { |
151 | pub(crate) source_item_id: SourceFileItemId, | 141 | pub(super) source_item_id: SourceFileItemId, |
152 | pub(crate) name: Name, | 142 | pub(super) name: Name, |
153 | pub(crate) kind: DefKind, | 143 | pub(super) kind: DefKind, |
154 | } | 144 | } |
155 | 145 | ||
156 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | 146 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] |
157 | pub(crate) enum DefKind { | 147 | pub(super) enum DefKind { |
158 | Function, | 148 | Function, |
159 | Struct, | 149 | Struct, |
160 | Enum, | 150 | Enum, |
@@ -165,16 +155,15 @@ pub(crate) enum DefKind { | |||
165 | } | 155 | } |
166 | 156 | ||
167 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 157 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
168 | pub(crate) struct Macro(RawId); | 158 | pub(super) struct Macro(RawId); |
169 | impl_arena_id!(Macro); | 159 | impl_arena_id!(Macro); |
170 | 160 | ||
171 | #[derive(Debug, PartialEq, Eq)] | 161 | #[derive(Debug, PartialEq, Eq)] |
172 | pub(crate) struct MacroData { | 162 | pub(super) struct MacroData { |
173 | pub(crate) source_item_id: SourceFileItemId, | 163 | pub(super) source_item_id: SourceFileItemId, |
174 | pub(crate) path: Path, | 164 | pub(super) path: Path, |
175 | pub(crate) name: Option<Name>, | 165 | pub(super) name: Option<Name>, |
176 | pub(crate) arg: tt::Subtree, | 166 | pub(super) export: bool, |
177 | pub(crate) export: bool, | ||
178 | } | 167 | } |
179 | 168 | ||
180 | struct RawItemsCollector { | 169 | struct RawItemsCollector { |
@@ -291,18 +280,15 @@ impl RawItemsCollector { | |||
291 | } | 280 | } |
292 | 281 | ||
293 | fn add_macro(&mut self, current_module: Option<Module>, m: &ast::MacroCall) { | 282 | fn add_macro(&mut self, current_module: Option<Module>, m: &ast::MacroCall) { |
294 | let (path, arg) = match ( | 283 | let path = match m.path().and_then(Path::from_ast) { |
295 | m.path().and_then(Path::from_ast), | 284 | Some(it) => it, |
296 | m.token_tree().and_then(mbe::ast_to_token_tree), | ||
297 | ) { | ||
298 | (Some(path), Some((token_tree, _token_map))) => (path, token_tree), | ||
299 | _ => return, | 285 | _ => return, |
300 | }; | 286 | }; |
301 | 287 | ||
302 | let name = m.name().map(|it| it.as_name()); | 288 | let name = m.name().map(|it| it.as_name()); |
303 | let source_item_id = self.source_file_items.id_of_unchecked(m.syntax()); | 289 | let source_item_id = self.source_file_items.id_of_unchecked(m.syntax()); |
304 | let export = m.has_atom_attr("macro_export"); | 290 | let export = m.has_atom_attr("macro_export"); |
305 | let m = self.raw_items.macros.alloc(MacroData { source_item_id, path, arg, name, export }); | 291 | let m = self.raw_items.macros.alloc(MacroData { source_item_id, path, name, export }); |
306 | self.push_item(current_module, RawItem::Macro(m)); | 292 | self.push_item(current_module, RawItem::Macro(m)); |
307 | } | 293 | } |
308 | 294 | ||