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/attr.rs | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) (limited to 'crates/ra_hir_def/src/attr.rs') diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs index 0e961ca12..7a9d0fdf4 100644 --- a/crates/ra_hir_def/src/attr.rs +++ b/crates/ra_hir_def/src/attr.rs @@ -1,6 +1,6 @@ //! A higher level attributes based on TokenTree, with also some shortcuts. -use std::sync::Arc; +use std::{ops, sync::Arc}; use hir_expand::hygiene::Hygiene; use mbe::ast_to_token_tree; @@ -13,6 +13,28 @@ use tt::Subtree; use crate::path::Path; +#[derive(Default, Debug, Clone, PartialEq, Eq)] +pub struct Attrs { + entries: Option>, +} + +impl ops::Deref for Attrs { + type Target = [Attr]; + + fn deref(&self) -> &[Attr] { + match &self.entries { + Some(it) => &*it, + None => &[], + } + } +} + +impl Attrs { + pub fn has_atom(&self, atom: &str) -> bool { + self.iter().any(|it| it.is_simple_atom(atom)) + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub struct Attr { pub(crate) path: Path, @@ -43,13 +65,15 @@ impl Attr { Some(Attr { path, input }) } - pub fn from_attrs_owner(owner: &dyn AttrsOwner, hygiene: &Hygiene) -> Option> { + pub fn from_attrs_owner(owner: &dyn AttrsOwner, hygiene: &Hygiene) -> Attrs { let mut attrs = owner.attrs().peekable(); - if attrs.peek().is_none() { + let entries = if attrs.peek().is_none() { // Avoid heap allocation - return None; - } - Some(attrs.flat_map(|ast| Attr::from_src(ast, hygiene)).collect()) + None + } else { + Some(attrs.flat_map(|ast| Attr::from_src(ast, hygiene)).collect()) + }; + Attrs { entries } } pub fn is_simple_atom(&self, name: &str) -> bool { -- cgit v1.2.3 From 552ba868afc8f72202ac834d07bbeb330aca007d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 11:14:10 +0300 Subject: Move attrs query to hir_def --- crates/ra_hir_def/src/attr.rs | 66 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir_def/src/attr.rs') diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs index 7a9d0fdf4..ce397f6b0 100644 --- a/crates/ra_hir_def/src/attr.rs +++ b/crates/ra_hir_def/src/attr.rs @@ -2,7 +2,7 @@ use std::{ops, sync::Arc}; -use hir_expand::hygiene::Hygiene; +use hir_expand::{either::Either, hygiene::Hygiene, AstId}; use mbe::ast_to_token_tree; use ra_cfg::CfgOptions; use ra_syntax::{ @@ -11,7 +11,9 @@ use ra_syntax::{ }; use tt::Subtree; -use crate::path::Path; +use crate::{ + db::DefDatabase2, path::Path, AdtId, AstItemDef, AttrDefId, HasChildSource, HasSource, Lookup, +}; #[derive(Default, Debug, Clone, PartialEq, Eq)] pub struct Attrs { @@ -30,6 +32,46 @@ impl ops::Deref for Attrs { } impl Attrs { + pub(crate) fn attrs_query(db: &impl DefDatabase2, def: AttrDefId) -> Attrs { + match def { + AttrDefId::ModuleId(module) => { + let def_map = db.crate_def_map(module.krate); + let src = match def_map[module.module_id].declaration_source(db) { + Some(it) => it, + None => return Attrs::default(), + }; + let hygiene = Hygiene::new(db, src.file_id); + Attr::from_attrs_owner(&src.value, &hygiene) + } + AttrDefId::StructFieldId(it) => { + let src = it.parent.child_source(db); + match &src.value[it.local_id] { + Either::A(_tuple) => Attrs::default(), + Either::B(record) => { + let hygiene = Hygiene::new(db, src.file_id); + Attr::from_attrs_owner(record, &hygiene) + } + } + } + AttrDefId::AdtId(it) => match it { + AdtId::StructId(it) => attrs_from_ast(it.0.lookup_intern(db).ast_id, db), + AdtId::EnumId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), + AdtId::UnionId(it) => attrs_from_ast(it.0.lookup_intern(db).ast_id, db), + }, + AttrDefId::EnumVariantId(it) => { + let src = it.parent.child_source(db); + let hygiene = Hygiene::new(db, src.file_id); + Attr::from_attrs_owner(&src.value[it.local_id], &hygiene) + } + AttrDefId::StaticId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), + AttrDefId::ConstId(it) => attrs_from_loc(it.lookup(db), db), + AttrDefId::FunctionId(it) => attrs_from_loc(it.lookup(db), db), + AttrDefId::TraitId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), + AttrDefId::TypeAliasId(it) => attrs_from_loc(it.lookup(db), db), + AttrDefId::MacroDefId(it) => attrs_from_ast(it.ast_id, db), + } + } + pub fn has_atom(&self, atom: &str) -> bool { self.iter().any(|it| it.is_simple_atom(atom)) } @@ -106,3 +148,23 @@ impl Attr { cfg_options.is_cfg_enabled(self.as_cfg()?) } } + +fn attrs_from_ast(src: AstId, db: &D) -> Attrs +where + N: ast::AttrsOwner, + D: DefDatabase2, +{ + let hygiene = Hygiene::new(db, src.file_id()); + Attr::from_attrs_owner(&src.to_node(db), &hygiene) +} + +fn attrs_from_loc(node: T, db: &D) -> Attrs +where + T: HasSource, + T::Value: ast::AttrsOwner, + D: DefDatabase2, +{ + let src = node.source(db); + let hygiene = Hygiene::new(db, src.file_id); + Attr::from_attrs_owner(&src.value, &hygiene) +} -- cgit v1.2.3 From 1a9986496387544c048475765ac64e83518437af Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 12:01:56 +0300 Subject: Use attrs rather than syntax for lang items --- crates/ra_hir_def/src/attr.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'crates/ra_hir_def/src/attr.rs') diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs index ce397f6b0..eee5e44bf 100644 --- a/crates/ra_hir_def/src/attr.rs +++ b/crates/ra_hir_def/src/attr.rs @@ -53,28 +53,38 @@ impl Attrs { } } } - AttrDefId::AdtId(it) => match it { - AdtId::StructId(it) => attrs_from_ast(it.0.lookup_intern(db).ast_id, db), - AdtId::EnumId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), - AdtId::UnionId(it) => attrs_from_ast(it.0.lookup_intern(db).ast_id, db), - }, AttrDefId::EnumVariantId(it) => { let src = it.parent.child_source(db); let hygiene = Hygiene::new(db, src.file_id); Attr::from_attrs_owner(&src.value[it.local_id], &hygiene) } + AttrDefId::AdtId(it) => match it { + AdtId::StructId(it) => attrs_from_ast(it.0.lookup_intern(db).ast_id, db), + AdtId::EnumId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), + AdtId::UnionId(it) => attrs_from_ast(it.0.lookup_intern(db).ast_id, db), + }, AttrDefId::StaticId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), + AttrDefId::TraitId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), + AttrDefId::MacroDefId(it) => attrs_from_ast(it.ast_id, db), + AttrDefId::ImplId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), AttrDefId::ConstId(it) => attrs_from_loc(it.lookup(db), db), AttrDefId::FunctionId(it) => attrs_from_loc(it.lookup(db), db), - AttrDefId::TraitId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), AttrDefId::TypeAliasId(it) => attrs_from_loc(it.lookup(db), db), - AttrDefId::MacroDefId(it) => attrs_from_ast(it.ast_id, db), } } pub fn has_atom(&self, atom: &str) -> bool { self.iter().any(|it| it.is_simple_atom(atom)) } + + pub fn find_string_value(&self, key: &str) -> Option { + self.iter().filter(|attr| attr.is_simple_atom(key)).find_map(|attr| { + match attr.input.as_ref()? { + AttrInput::Literal(it) => Some(it.clone()), + _ => None, + } + }) + } } #[derive(Debug, Clone, PartialEq, Eq)] -- 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/attr.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'crates/ra_hir_def/src/attr.rs') diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs index eee5e44bf..48ce8cd93 100644 --- a/crates/ra_hir_def/src/attr.rs +++ b/crates/ra_hir_def/src/attr.rs @@ -12,7 +12,7 @@ use ra_syntax::{ use tt::Subtree; use crate::{ - db::DefDatabase2, path::Path, AdtId, AstItemDef, AttrDefId, HasChildSource, HasSource, Lookup, + db::DefDatabase, path::Path, AdtId, AstItemDef, AttrDefId, HasChildSource, HasSource, Lookup, }; #[derive(Default, Debug, Clone, PartialEq, Eq)] @@ -32,7 +32,7 @@ impl ops::Deref for Attrs { } impl Attrs { - pub(crate) fn attrs_query(db: &impl DefDatabase2, def: AttrDefId) -> Attrs { + pub(crate) fn attrs_query(db: &impl DefDatabase, def: AttrDefId) -> Attrs { match def { AttrDefId::ModuleId(module) => { let def_map = db.crate_def_map(module.krate); @@ -162,7 +162,7 @@ impl Attr { fn attrs_from_ast(src: AstId, db: &D) -> Attrs where N: ast::AttrsOwner, - D: DefDatabase2, + D: DefDatabase, { let hygiene = Hygiene::new(db, src.file_id()); Attr::from_attrs_owner(&src.to_node(db), &hygiene) @@ -172,7 +172,7 @@ fn attrs_from_loc(node: T, db: &D) -> Attrs where T: HasSource, T::Value: ast::AttrsOwner, - D: DefDatabase2, + D: DefDatabase, { let src = node.source(db); let hygiene = Hygiene::new(db, src.file_id); -- cgit v1.2.3 From e0b06cb672b7aae770fea24e4a5efdbec8cbf5c6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 15:13:56 +0300 Subject: Switch to StaticLoc for statics --- crates/ra_hir_def/src/attr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir_def/src/attr.rs') diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs index 48ce8cd93..87f411599 100644 --- a/crates/ra_hir_def/src/attr.rs +++ b/crates/ra_hir_def/src/attr.rs @@ -63,11 +63,11 @@ impl Attrs { AdtId::EnumId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), AdtId::UnionId(it) => attrs_from_ast(it.0.lookup_intern(db).ast_id, db), }, - AttrDefId::StaticId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), AttrDefId::TraitId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), AttrDefId::MacroDefId(it) => attrs_from_ast(it.ast_id, db), AttrDefId::ImplId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), AttrDefId::ConstId(it) => attrs_from_loc(it.lookup(db), db), + AttrDefId::StaticId(it) => attrs_from_loc(it.lookup(db), db), AttrDefId::FunctionId(it) => attrs_from_loc(it.lookup(db), db), AttrDefId::TypeAliasId(it) => attrs_from_loc(it.lookup(db), db), } -- cgit v1.2.3 From 8e36cb586038e2c12e6eceae57f7a95684fc6c6d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 15:28:45 +0300 Subject: Simplify --- crates/ra_hir_def/src/attr.rs | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) (limited to 'crates/ra_hir_def/src/attr.rs') diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs index 87f411599..7d8f0d915 100644 --- a/crates/ra_hir_def/src/attr.rs +++ b/crates/ra_hir_def/src/attr.rs @@ -144,17 +144,7 @@ impl Attr { } } - pub fn as_path(&self) -> Option<&SmolStr> { - if !self.is_simple_atom("path") { - return None; - } - match &self.input { - Some(AttrInput::Literal(it)) => Some(it), - _ => None, - } - } - - pub fn is_cfg_enabled(&self, cfg_options: &CfgOptions) -> Option { + pub(crate) fn is_cfg_enabled(&self, cfg_options: &CfgOptions) -> Option { cfg_options.is_cfg_enabled(self.as_cfg()?) } } -- 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/attr.rs | 54 +++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 27 deletions(-) (limited to 'crates/ra_hir_def/src/attr.rs') diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs index 7d8f0d915..5c1b151f7 100644 --- a/crates/ra_hir_def/src/attr.rs +++ b/crates/ra_hir_def/src/attr.rs @@ -2,7 +2,7 @@ use std::{ops, sync::Arc}; -use hir_expand::{either::Either, hygiene::Hygiene, AstId}; +use hir_expand::{either::Either, hygiene::Hygiene, AstId, Source}; use mbe::ast_to_token_tree; use ra_cfg::CfgOptions; use ra_syntax::{ @@ -40,23 +40,19 @@ impl Attrs { Some(it) => it, None => return Attrs::default(), }; - let hygiene = Hygiene::new(db, src.file_id); - Attr::from_attrs_owner(&src.value, &hygiene) + Attrs::from_attrs_owner(db, src.as_ref().map(|it| it as &dyn AttrsOwner)) } AttrDefId::StructFieldId(it) => { let src = it.parent.child_source(db); match &src.value[it.local_id] { Either::A(_tuple) => Attrs::default(), - Either::B(record) => { - let hygiene = Hygiene::new(db, src.file_id); - Attr::from_attrs_owner(record, &hygiene) - } + Either::B(record) => Attrs::from_attrs_owner(db, src.with_value(record)), } } - AttrDefId::EnumVariantId(it) => { - let src = it.parent.child_source(db); - let hygiene = Hygiene::new(db, src.file_id); - Attr::from_attrs_owner(&src.value[it.local_id], &hygiene) + AttrDefId::EnumVariantId(var_id) => { + let src = var_id.parent.child_source(db); + let src = src.as_ref().map(|it| &it[var_id.local_id]); + Attrs::from_attrs_owner(db, src.map(|it| it as &dyn AttrsOwner)) } AttrDefId::AdtId(it) => match it { AdtId::StructId(it) => attrs_from_ast(it.0.lookup_intern(db).ast_id, db), @@ -73,6 +69,22 @@ impl Attrs { } } + fn from_attrs_owner(db: &impl DefDatabase, owner: Source<&dyn AttrsOwner>) -> Attrs { + let hygiene = Hygiene::new(db, owner.file_id); + Attrs::new(owner.value, &hygiene) + } + + pub(crate) fn new(owner: &dyn AttrsOwner, hygiene: &Hygiene) -> Attrs { + let mut attrs = owner.attrs().peekable(); + let entries = if attrs.peek().is_none() { + // Avoid heap allocation + None + } else { + Some(attrs.flat_map(|ast| Attr::from_src(ast, hygiene)).collect()) + }; + Attrs { entries } + } + pub fn has_atom(&self, atom: &str) -> bool { self.iter().any(|it| it.is_simple_atom(atom)) } @@ -100,7 +112,7 @@ pub enum AttrInput { } impl Attr { - pub(crate) fn from_src(ast: ast::Attr, hygiene: &Hygiene) -> Option { + fn from_src(ast: ast::Attr, hygiene: &Hygiene) -> Option { let path = Path::from_src(ast.path()?, hygiene)?; let input = match ast.input() { None => None, @@ -117,17 +129,6 @@ impl Attr { Some(Attr { path, input }) } - pub fn from_attrs_owner(owner: &dyn AttrsOwner, hygiene: &Hygiene) -> Attrs { - let mut attrs = owner.attrs().peekable(); - let entries = if attrs.peek().is_none() { - // Avoid heap allocation - None - } else { - Some(attrs.flat_map(|ast| Attr::from_src(ast, hygiene)).collect()) - }; - Attrs { entries } - } - pub fn is_simple_atom(&self, name: &str) -> bool { // FIXME: Avoid cloning self.path.as_ident().map_or(false, |s| s.to_string() == name) @@ -154,8 +155,8 @@ where N: ast::AttrsOwner, D: DefDatabase, { - let hygiene = Hygiene::new(db, src.file_id()); - Attr::from_attrs_owner(&src.to_node(db), &hygiene) + let src = Source::new(src.file_id(), src.to_node(db)); + Attrs::from_attrs_owner(db, src.as_ref().map(|it| it as &dyn AttrsOwner)) } fn attrs_from_loc(node: T, db: &D) -> Attrs @@ -165,6 +166,5 @@ where D: DefDatabase, { let src = node.source(db); - let hygiene = Hygiene::new(db, src.file_id); - Attr::from_attrs_owner(&src.value, &hygiene) + Attrs::from_attrs_owner(db, src.as_ref().map(|it| it as &dyn AttrsOwner)) } -- cgit v1.2.3 From 4b74fb1d896ce5a1c8c4c4bf73ad2940fb86abc5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 16:03:02 +0300 Subject: Nicer API for attrs --- crates/ra_hir_def/src/attr.rs | 52 ++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 25 deletions(-) (limited to 'crates/ra_hir_def/src/attr.rs') diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs index 5c1b151f7..53456fc08 100644 --- a/crates/ra_hir_def/src/attr.rs +++ b/crates/ra_hir_def/src/attr.rs @@ -4,7 +4,6 @@ use std::{ops, sync::Arc}; use hir_expand::{either::Either, hygiene::Hygiene, AstId, Source}; use mbe::ast_to_token_tree; -use ra_cfg::CfgOptions; use ra_syntax::{ ast::{self, AstNode, AttrsOwner}, SmolStr, @@ -85,17 +84,8 @@ impl Attrs { Attrs { entries } } - pub fn has_atom(&self, atom: &str) -> bool { - self.iter().any(|it| it.is_simple_atom(atom)) - } - - pub fn find_string_value(&self, key: &str) -> Option { - self.iter().filter(|attr| attr.is_simple_atom(key)).find_map(|attr| { - match attr.input.as_ref()? { - AttrInput::Literal(it) => Some(it.clone()), - _ => None, - } - }) + pub fn by_key(&self, key: &'static str) -> AttrQuery<'_> { + AttrQuery { attrs: self, key } } } @@ -128,25 +118,37 @@ impl Attr { Some(Attr { path, input }) } +} + +pub struct AttrQuery<'a> { + attrs: &'a Attrs, + key: &'static str, +} - pub fn is_simple_atom(&self, name: &str) -> bool { - // FIXME: Avoid cloning - self.path.as_ident().map_or(false, |s| s.to_string() == name) +impl<'a> AttrQuery<'a> { + pub fn tt_values(self) -> impl Iterator { + self.attrs().filter_map(|attr| match attr.input.as_ref()? { + AttrInput::TokenTree(it) => Some(it), + _ => None, + }) } - // FIXME: handle cfg_attr :-) - pub fn as_cfg(&self) -> Option<&Subtree> { - if !self.is_simple_atom("cfg") { - return None; - } - match &self.input { - Some(AttrInput::TokenTree(subtree)) => Some(subtree), + pub fn string_value(self) -> Option<&'a SmolStr> { + self.attrs().find_map(|attr| match attr.input.as_ref()? { + AttrInput::Literal(it) => Some(it), _ => None, - } + }) + } + + pub fn exists(self) -> bool { + self.attrs().next().is_some() } - pub(crate) fn is_cfg_enabled(&self, cfg_options: &CfgOptions) -> Option { - cfg_options.is_cfg_enabled(self.as_cfg()?) + fn attrs(self) -> impl Iterator { + let key = self.key; + self.attrs + .iter() + .filter(move |attr| attr.path.as_ident().map_or(false, |s| s.to_string() == key)) } } -- cgit v1.2.3