From e42f9627664cc3c44094e1c4f985270fbfd592b1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 22 Nov 2019 11:27:47 +0300 Subject: Encapsulate Attrs --- crates/ra_hir_def/src/nameres/raw.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) (limited to 'crates/ra_hir_def/src/nameres/raw.rs') diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 7c68fd638..55a9634f8 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -16,7 +16,12 @@ use ra_syntax::{ }; use test_utils::tested_by; -use crate::{attr::Attr, db::DefDatabase2, path::Path, FileAstId, HirFileId, ModuleSource, Source}; +use crate::{ + attr::{Attr, Attrs}, + db::DefDatabase2, + path::Path, + FileAstId, HirFileId, ModuleSource, Source, +}; /// `RawItems` is a set of top-level items in a file (except for impls). /// @@ -129,21 +134,12 @@ impl Index for RawItems { } } -// Avoid heap allocation on items without attributes. -type Attrs = Option>; - #[derive(Debug, PartialEq, Eq, Clone)] pub(super) struct RawItem { - attrs: Attrs, + pub(super) attrs: Attrs, pub(super) kind: RawItemKind, } -impl RawItem { - pub(super) fn attrs(&self) -> &[Attr] { - self.attrs.as_ref().map_or(&[], |it| &*it) - } -} - #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub(super) enum RawItemKind { Module(Module), -- cgit v1.2.3 From fc1e543f7abb69b8cab308410fa0a127950ee1c5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 14:44:43 +0300 Subject: Get rid of DefDatabase2 --- crates/ra_hir_def/src/nameres/raw.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir_def/src/nameres/raw.rs') diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 55a9634f8..7618cb059 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -18,7 +18,7 @@ use test_utils::tested_by; use crate::{ attr::{Attr, Attrs}, - db::DefDatabase2, + db::DefDatabase, path::Path, FileAstId, HirFileId, ModuleSource, Source, }; @@ -67,14 +67,14 @@ impl ImportSourceMap { impl RawItems { pub(crate) fn raw_items_query( - db: &(impl DefDatabase2 + AstDatabase), + db: &(impl DefDatabase + AstDatabase), file_id: HirFileId, ) -> Arc { db.raw_items_with_source_map(file_id).0 } pub(crate) fn raw_items_with_source_map_query( - db: &(impl DefDatabase2 + AstDatabase), + db: &(impl DefDatabase + AstDatabase), file_id: HirFileId, ) -> (Arc, Arc) { let mut collector = RawItemsCollector { -- cgit v1.2.3 From dd5c2dc5bf9e9dee863bd79105b1782b654221f7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 16:49:05 +0300 Subject: Move ImportId --- crates/ra_hir_def/src/nameres/raw.rs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'crates/ra_hir_def/src/nameres/raw.rs') diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 7618cb059..8ee6f54cd 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -20,7 +20,7 @@ use crate::{ attr::{Attr, Attrs}, db::DefDatabase, path::Path, - FileAstId, HirFileId, ModuleSource, Source, + FileAstId, HirFileId, LocalImportId, ModuleSource, Source, }; /// `RawItems` is a set of top-level items in a file (except for impls). @@ -30,7 +30,7 @@ use crate::{ #[derive(Debug, Default, PartialEq, Eq)] pub struct RawItems { modules: Arena, - imports: Arena, + imports: Arena, defs: Arena, macros: Arena, impls: Arena, @@ -40,7 +40,7 @@ pub struct RawItems { #[derive(Debug, Default, PartialEq, Eq)] pub struct ImportSourceMap { - map: ArenaMap, + map: ArenaMap, } type ImportSourcePtr = Either, AstPtr>; @@ -51,11 +51,11 @@ fn to_node(ptr: ImportSourcePtr, file: &SourceFile) -> ImportSource { } impl ImportSourceMap { - fn insert(&mut self, import: ImportId, ptr: ImportSourcePtr) { + fn insert(&mut self, import: LocalImportId, ptr: ImportSourcePtr) { self.map.insert(import, ptr) } - pub fn get(&self, source: &ModuleSource, import: ImportId) -> ImportSource { + pub fn get(&self, source: &ModuleSource, import: LocalImportId) -> ImportSource { let file = match source { ModuleSource::SourceFile(file) => file.clone(), ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(), @@ -106,9 +106,9 @@ impl Index for RawItems { } } -impl Index for RawItems { +impl Index for RawItems { type Output = ImportData; - fn index(&self, idx: ImportId) -> &ImportData { + fn index(&self, idx: LocalImportId) -> &ImportData { &self.imports[idx] } } @@ -143,7 +143,7 @@ pub(super) struct RawItem { #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub(super) enum RawItemKind { Module(Module), - Import(ImportId), + Import(LocalImportId), Def(Def), Macro(Macro), Impl(Impl), @@ -159,10 +159,6 @@ pub(super) enum ModuleData { Definition { name: Name, ast_id: FileAstId, items: Vec }, } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct ImportId(RawId); -impl_arena_id!(ImportId); - #[derive(Debug, Clone, PartialEq, Eq)] pub struct ImportData { pub(super) path: Path, -- cgit v1.2.3 From ffc2325d194d2523456484a7dec1f175c729c1b5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 17:06:04 +0300 Subject: Move ModuleSource back to hir --- crates/ra_hir_def/src/nameres/raw.rs | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) (limited to 'crates/ra_hir_def/src/nameres/raw.rs') diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 8ee6f54cd..552cbe544 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -12,7 +12,7 @@ use hir_expand::{ use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId}; use ra_syntax::{ ast::{self, AttrsOwner, NameOwner}, - AstNode, AstPtr, SourceFile, + AstNode, AstPtr, }; use test_utils::tested_by; @@ -20,7 +20,7 @@ use crate::{ attr::{Attr, Attrs}, db::DefDatabase, path::Path, - FileAstId, HirFileId, LocalImportId, ModuleSource, Source, + FileAstId, HirFileId, LocalImportId, Source, }; /// `RawItems` is a set of top-level items in a file (except for impls). @@ -44,24 +44,14 @@ pub struct ImportSourceMap { } type ImportSourcePtr = Either, AstPtr>; -type ImportSource = Either; - -fn to_node(ptr: ImportSourcePtr, file: &SourceFile) -> ImportSource { - ptr.map(|ptr| ptr.to_node(file.syntax()), |ptr| ptr.to_node(file.syntax())) -} impl ImportSourceMap { fn insert(&mut self, import: LocalImportId, ptr: ImportSourcePtr) { self.map.insert(import, ptr) } - pub fn get(&self, source: &ModuleSource, import: LocalImportId) -> ImportSource { - let file = match source { - ModuleSource::SourceFile(file) => file.clone(), - ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(), - }; - - to_node(self.map[import], &file) + pub fn get(&self, import: LocalImportId) -> ImportSourcePtr { + self.map[import].clone() } } -- cgit v1.2.3 From 1956d57ed4896bb29dfcfaed2a5291ec69251f52 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 15:50:45 +0300 Subject: Slightly reduce code duplication --- crates/ra_hir_def/src/nameres/raw.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'crates/ra_hir_def/src/nameres/raw.rs') diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 552cbe544..198578753 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -17,10 +17,7 @@ use ra_syntax::{ use test_utils::tested_by; use crate::{ - attr::{Attr, Attrs}, - db::DefDatabase, - path::Path, - FileAstId, HirFileId, LocalImportId, Source, + attr::Attrs, db::DefDatabase, path::Path, FileAstId, HirFileId, LocalImportId, Source, }; /// `RawItems` is a set of top-level items in a file (except for impls). @@ -407,6 +404,6 @@ impl RawItemsCollector { } fn parse_attrs(&self, item: &impl ast::AttrsOwner) -> Attrs { - Attr::from_attrs_owner(item, &self.hygiene) + Attrs::new(item, &self.hygiene) } } -- cgit v1.2.3 From 21cfa6d529babf868f897b943d67561ea752b9e5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 17:34:36 +0300 Subject: Some docs --- crates/ra_hir_def/src/nameres/raw.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir_def/src/nameres/raw.rs') diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 198578753..2ec84f2cc 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -1,4 +1,9 @@ -//! FIXME: write short doc here +//! Lowers syntax tree of a rust file into a raw representation of containing +//! items, *without* attaching them to a module structure. +//! +//! That is, raw items don't have semantics, just as syntax, but, unlike syntax, +//! they don't change with trivial source code edits, making them a great tool +//! for building salsa recomputation firewalls. use std::{ops::Index, sync::Arc}; -- cgit v1.2.3 From 855a629b1436e7d3f2a9ae47d242d69d1fd7327e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 17:49:49 +0300 Subject: Use Trace in raw_items --- crates/ra_hir_def/src/nameres/raw.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'crates/ra_hir_def/src/nameres/raw.rs') 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::{ use test_utils::tested_by; use crate::{ - attr::Attrs, db::DefDatabase, path::Path, FileAstId, HirFileId, LocalImportId, Source, + attr::Attrs, db::DefDatabase, path::Path, trace::Trace, FileAstId, HirFileId, LocalImportId, + Source, }; /// `RawItems` is a set of top-level items in a file (except for impls). @@ -48,10 +49,6 @@ pub struct ImportSourceMap { type ImportSourcePtr = Either, AstPtr>; impl ImportSourceMap { - fn insert(&mut self, import: LocalImportId, ptr: ImportSourcePtr) { - self.map.insert(import, ptr) - } - pub fn get(&self, import: LocalImportId) -> ImportSourcePtr { self.map[import].clone() } @@ -72,7 +69,7 @@ impl RawItems { let mut collector = RawItemsCollector { raw_items: RawItems::default(), source_ast_id_map: db.ast_id_map(file_id), - source_map: ImportSourceMap::default(), + imports: Trace::new(), file_id, hygiene: Hygiene::new(db, file_id), }; @@ -83,7 +80,11 @@ impl RawItems { collector.process_module(None, item_list); } } - (Arc::new(collector.raw_items), Arc::new(collector.source_map)) + let mut raw_items = collector.raw_items; + let (arena, map) = collector.imports.into_arena_and_map(); + raw_items.imports = arena; + let source_map = ImportSourceMap { map }; + (Arc::new(raw_items), Arc::new(source_map)) } pub(super) fn items(&self) -> &[RawItem] { @@ -207,8 +208,8 @@ pub(super) struct ImplData { struct RawItemsCollector { raw_items: RawItems, + imports: Trace, source_ast_id_map: Arc, - source_map: ImportSourceMap, file_id: HirFileId, hygiene: Hygiene, } @@ -392,8 +393,7 @@ impl RawItemsCollector { data: ImportData, source: ImportSourcePtr, ) { - let import = self.raw_items.imports.alloc(data); - self.source_map.insert(import, source); + let import = self.imports.alloc(|| source, || data); self.push_item(current_module, attrs, RawItemKind::Import(import)) } -- cgit v1.2.3