aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r--crates/ra_hir_def/src/adt.rs6
-rw-r--r--crates/ra_hir_def/src/nameres/raw.rs20
-rw-r--r--crates/ra_hir_def/src/trace.rs42
3 files changed, 39 insertions, 29 deletions
diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs
index 20e9a1eb5..a43f553aa 100644
--- a/crates/ra_hir_def/src/adt.rs
+++ b/crates/ra_hir_def/src/adt.rs
@@ -95,7 +95,7 @@ fn lower_enum(
95 name: var.name().map(|it| it.as_name()), 95 name: var.name().map(|it| it.as_name()),
96 variant_data: Arc::new(VariantData::new(var.kind())), 96 variant_data: Arc::new(VariantData::new(var.kind())),
97 }, 97 },
98 ) 98 );
99 } 99 }
100} 100}
101 101
@@ -160,7 +160,7 @@ fn lower_struct(
160 name: Name::new_tuple_field(i), 160 name: Name::new_tuple_field(i),
161 type_ref: TypeRef::from_ast_opt(fd.type_ref()), 161 type_ref: TypeRef::from_ast_opt(fd.type_ref()),
162 }, 162 },
163 ) 163 );
164 } 164 }
165 StructKind::Tuple 165 StructKind::Tuple
166 } 166 }
@@ -172,7 +172,7 @@ fn lower_struct(
172 name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing), 172 name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing),
173 type_ref: TypeRef::from_ast_opt(fd.ascribed_type()), 173 type_ref: TypeRef::from_ast_opt(fd.ascribed_type()),
174 }, 174 },
175 ) 175 );
176 } 176 }
177 StructKind::Record 177 StructKind::Record
178 } 178 }
diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs
index 2ec84f2cc..401af031c 100644
--- a/crates/ra_hir_def/src/nameres/raw.rs
+++ b/crates/ra_hir_def/src/nameres/raw.rs
@@ -22,7 +22,8 @@ use ra_syntax::{
22use test_utils::tested_by; 22use test_utils::tested_by;
23 23
24use crate::{ 24use crate::{
25 attr::Attrs, db::DefDatabase, path::Path, FileAstId, HirFileId, LocalImportId, Source, 25 attr::Attrs, db::DefDatabase, path::Path, trace::Trace, FileAstId, HirFileId, LocalImportId,
26 Source,
26}; 27};
27 28
28/// `RawItems` is a set of top-level items in a file (except for impls). 29/// `RawItems` is a set of top-level items in a file (except for impls).
@@ -48,10 +49,6 @@ pub struct ImportSourceMap {
48type ImportSourcePtr = Either<AstPtr<ast::UseTree>, AstPtr<ast::ExternCrateItem>>; 49type ImportSourcePtr = Either<AstPtr<ast::UseTree>, AstPtr<ast::ExternCrateItem>>;
49 50
50impl ImportSourceMap { 51impl ImportSourceMap {
51 fn insert(&mut self, import: LocalImportId, ptr: ImportSourcePtr) {
52 self.map.insert(import, ptr)
53 }
54
55 pub fn get(&self, import: LocalImportId) -> ImportSourcePtr { 52 pub fn get(&self, import: LocalImportId) -> ImportSourcePtr {
56 self.map[import].clone() 53 self.map[import].clone()
57 } 54 }
@@ -72,7 +69,7 @@ impl RawItems {
72 let mut collector = RawItemsCollector { 69 let mut collector = RawItemsCollector {
73 raw_items: RawItems::default(), 70 raw_items: RawItems::default(),
74 source_ast_id_map: db.ast_id_map(file_id), 71 source_ast_id_map: db.ast_id_map(file_id),
75 source_map: ImportSourceMap::default(), 72 imports: Trace::new(),
76 file_id, 73 file_id,
77 hygiene: Hygiene::new(db, file_id), 74 hygiene: Hygiene::new(db, file_id),
78 }; 75 };
@@ -83,7 +80,11 @@ impl RawItems {
83 collector.process_module(None, item_list); 80 collector.process_module(None, item_list);
84 } 81 }
85 } 82 }
86 (Arc::new(collector.raw_items), Arc::new(collector.source_map)) 83 let mut raw_items = collector.raw_items;
84 let (arena, map) = collector.imports.into_arena_and_map();
85 raw_items.imports = arena;
86 let source_map = ImportSourceMap { map };
87 (Arc::new(raw_items), Arc::new(source_map))
87 } 88 }
88 89
89 pub(super) fn items(&self) -> &[RawItem] { 90 pub(super) fn items(&self) -> &[RawItem] {
@@ -207,8 +208,8 @@ pub(super) struct ImplData {
207 208
208struct RawItemsCollector { 209struct RawItemsCollector {
209 raw_items: RawItems, 210 raw_items: RawItems,
211 imports: Trace<LocalImportId, ImportData, ImportSourcePtr>,
210 source_ast_id_map: Arc<AstIdMap>, 212 source_ast_id_map: Arc<AstIdMap>,
211 source_map: ImportSourceMap,
212 file_id: HirFileId, 213 file_id: HirFileId,
213 hygiene: Hygiene, 214 hygiene: Hygiene,
214} 215}
@@ -392,8 +393,7 @@ impl RawItemsCollector {
392 data: ImportData, 393 data: ImportData,
393 source: ImportSourcePtr, 394 source: ImportSourcePtr,
394 ) { 395 ) {
395 let import = self.raw_items.imports.alloc(data); 396 let import = self.imports.alloc(|| source, || data);
396 self.source_map.insert(import, source);
397 self.push_item(current_module, attrs, RawItemKind::Import(import)) 397 self.push_item(current_module, attrs, RawItemKind::Import(import))
398 } 398 }
399 399
diff --git a/crates/ra_hir_def/src/trace.rs b/crates/ra_hir_def/src/trace.rs
index fc26f5a48..2bcd707bc 100644
--- a/crates/ra_hir_def/src/trace.rs
+++ b/crates/ra_hir_def/src/trace.rs
@@ -12,38 +12,48 @@
12use ra_arena::{map::ArenaMap, Arena, ArenaId, RawId}; 12use ra_arena::{map::ArenaMap, Arena, ArenaId, RawId};
13 13
14pub(crate) struct Trace<ID: ArenaId, T, V> { 14pub(crate) struct Trace<ID: ArenaId, T, V> {
15 for_arena: bool, 15 arena: Option<Arena<ID, T>>,
16 arena: Arena<ID, T>, 16 map: Option<ArenaMap<ID, V>>,
17 map: ArenaMap<ID, V>,
18 len: u32, 17 len: u32,
19} 18}
20 19
21impl<ID: ra_arena::ArenaId, T, V> Trace<ID, T, V> { 20impl<ID: ra_arena::ArenaId + Copy, T, V> Trace<ID, T, V> {
21 pub(crate) fn new() -> Trace<ID, T, V> {
22 Trace { arena: Some(Arena::default()), map: Some(ArenaMap::default()), len: 0 }
23 }
24
22 pub(crate) fn new_for_arena() -> Trace<ID, T, V> { 25 pub(crate) fn new_for_arena() -> Trace<ID, T, V> {
23 Trace { for_arena: true, arena: Arena::default(), map: ArenaMap::default(), len: 0 } 26 Trace { arena: Some(Arena::default()), map: None, len: 0 }
24 } 27 }
25 28
26 pub(crate) fn new_for_map() -> Trace<ID, T, V> { 29 pub(crate) fn new_for_map() -> Trace<ID, T, V> {
27 Trace { for_arena: false, arena: Arena::default(), map: ArenaMap::default(), len: 0 } 30 Trace { arena: None, map: Some(ArenaMap::default()), len: 0 }
28 } 31 }
29 32
30 pub(crate) fn alloc(&mut self, value: impl Fn() -> V, data: impl Fn() -> T) { 33 pub(crate) fn alloc(&mut self, value: impl FnOnce() -> V, data: impl FnOnce() -> T) -> ID {
31 if self.for_arena { 34 let id = if let Some(arena) = &mut self.arena {
32 self.arena.alloc(data()); 35 arena.alloc(data())
33 } else { 36 } else {
34 let id = ID::from_raw(RawId::from(self.len)); 37 let id = ID::from_raw(RawId::from(self.len));
35 self.len += 1; 38 self.len += 1;
36 self.map.insert(id, value()); 39 id
40 };
41
42 if let Some(map) = &mut self.map {
43 map.insert(id, value());
37 } 44 }
45 id
46 }
47
48 pub(crate) fn into_arena(mut self) -> Arena<ID, T> {
49 self.arena.take().unwrap()
38 } 50 }
39 51
40 pub(crate) fn into_arena(self) -> Arena<ID, T> { 52 pub(crate) fn into_map(mut self) -> ArenaMap<ID, V> {
41 assert!(self.for_arena); 53 self.map.take().unwrap()
42 self.arena
43 } 54 }
44 55
45 pub(crate) fn into_map(self) -> ArenaMap<ID, V> { 56 pub(crate) fn into_arena_and_map(mut self) -> (Arena<ID, T>, ArenaMap<ID, V>) {
46 assert!(!self.for_arena); 57 (self.arena.take().unwrap(), self.map.take().unwrap())
47 self.map
48 } 58 }
49} 59}