2019-10-30 13:12:55 +00:00
|
|
|
//! A higher level attributes based on TokenTree, with also some shortcuts.
|
|
|
|
|
2019-11-22 08:27:47 +00:00
|
|
|
use std::{ops, sync::Arc};
|
2019-10-30 13:12:55 +00:00
|
|
|
|
2019-12-03 16:07:56 +00:00
|
|
|
use either::Either;
|
|
|
|
use hir_expand::{hygiene::Hygiene, AstId, InFile};
|
2019-10-30 13:12:55 +00:00
|
|
|
use mbe::ast_to_token_tree;
|
2020-07-23 14:22:17 +00:00
|
|
|
use ra_cfg::{CfgExpr, CfgOptions};
|
2019-10-30 13:12:55 +00:00
|
|
|
use ra_syntax::{
|
|
|
|
ast::{self, AstNode, AttrsOwner},
|
|
|
|
SmolStr,
|
|
|
|
};
|
|
|
|
use tt::Subtree;
|
|
|
|
|
2019-11-23 08:14:10 +00:00
|
|
|
use crate::{
|
2020-06-22 17:15:54 +00:00
|
|
|
db::DefDatabase,
|
|
|
|
item_tree::{ItemTreeId, ItemTreeNode},
|
|
|
|
nameres::ModuleSource,
|
|
|
|
path::ModPath,
|
|
|
|
src::HasChildSource,
|
2020-04-25 14:24:44 +00:00
|
|
|
AdtId, AttrDefId, Lookup,
|
2019-11-23 08:14:10 +00:00
|
|
|
};
|
2019-10-30 13:12:55 +00:00
|
|
|
|
2019-11-22 08:27:47 +00:00
|
|
|
#[derive(Default, Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct Attrs {
|
|
|
|
entries: Option<Arc<[Attr]>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Deref for Attrs {
|
|
|
|
type Target = [Attr];
|
|
|
|
|
|
|
|
fn deref(&self) -> &[Attr] {
|
|
|
|
match &self.entries {
|
|
|
|
Some(it) => &*it,
|
|
|
|
None => &[],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Attrs {
|
2020-06-23 17:42:19 +00:00
|
|
|
pub const EMPTY: Attrs = Attrs { entries: None };
|
|
|
|
|
2020-03-13 15:05:46 +00:00
|
|
|
pub(crate) fn attrs_query(db: &dyn DefDatabase, def: AttrDefId) -> Attrs {
|
2019-11-23 08:14:10 +00:00
|
|
|
match def {
|
|
|
|
AttrDefId::ModuleId(module) => {
|
|
|
|
let def_map = db.crate_def_map(module.krate);
|
2020-04-25 14:24:44 +00:00
|
|
|
let mod_data = &def_map[module.local_id];
|
|
|
|
match mod_data.declaration_source(db) {
|
|
|
|
Some(it) => {
|
|
|
|
Attrs::from_attrs_owner(db, it.as_ref().map(|it| it as &dyn AttrsOwner))
|
|
|
|
}
|
|
|
|
None => Attrs::from_attrs_owner(
|
|
|
|
db,
|
|
|
|
mod_data.definition_source(db).as_ref().map(|src| match src {
|
|
|
|
ModuleSource::SourceFile(file) => file as &dyn AttrsOwner,
|
|
|
|
ModuleSource::Module(module) => module as &dyn AttrsOwner,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
}
|
2019-11-23 08:14:10 +00:00
|
|
|
}
|
2020-04-25 12:23:34 +00:00
|
|
|
AttrDefId::FieldId(it) => {
|
2019-11-23 08:14:10 +00:00
|
|
|
let src = it.parent.child_source(db);
|
|
|
|
match &src.value[it.local_id] {
|
2019-12-03 16:07:56 +00:00
|
|
|
Either::Left(_tuple) => Attrs::default(),
|
|
|
|
Either::Right(record) => Attrs::from_attrs_owner(db, src.with_value(record)),
|
2019-11-23 08:14:10 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-24 12:50:45 +00:00
|
|
|
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))
|
2019-11-23 08:14:10 +00:00
|
|
|
}
|
2019-11-23 09:01:56 +00:00
|
|
|
AttrDefId::AdtId(it) => match it {
|
2020-06-22 17:15:54 +00:00
|
|
|
AdtId::StructId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
|
|
|
AdtId::EnumId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
|
|
|
AdtId::UnionId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
2019-11-23 09:01:56 +00:00
|
|
|
},
|
2020-06-22 17:15:54 +00:00
|
|
|
AttrDefId::TraitId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
2019-12-05 14:10:33 +00:00
|
|
|
AttrDefId::MacroDefId(it) => {
|
|
|
|
it.ast_id.map_or_else(Default::default, |ast_id| attrs_from_ast(ast_id, db))
|
|
|
|
}
|
2020-06-22 17:15:54 +00:00
|
|
|
AttrDefId::ImplId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
|
|
|
AttrDefId::ConstId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
|
|
|
AttrDefId::StaticId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
|
|
|
AttrDefId::FunctionId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
|
|
|
AttrDefId::TypeAliasId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
2019-11-23 08:14:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-21 08:48:42 +00:00
|
|
|
pub fn from_attrs_owner(db: &dyn DefDatabase, owner: InFile<&dyn AttrsOwner>) -> Attrs {
|
2020-03-13 15:05:46 +00:00
|
|
|
let hygiene = Hygiene::new(db.upcast(), owner.file_id);
|
2019-11-24 12:50:45 +00:00
|
|
|
Attrs::new(owner.value, &hygiene)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn new(owner: &dyn AttrsOwner, hygiene: &Hygiene) -> Attrs {
|
2020-05-30 18:21:06 +00:00
|
|
|
let docs = ast::CommentIter::from_syntax_node(owner.syntax()).doc_comment_text().map(
|
|
|
|
|docs_text| Attr {
|
|
|
|
input: Some(AttrInput::Literal(SmolStr::new(docs_text))),
|
|
|
|
path: ModPath::from(hir_expand::name!(doc)),
|
|
|
|
},
|
|
|
|
);
|
2019-11-24 12:50:45 +00:00
|
|
|
let mut attrs = owner.attrs().peekable();
|
|
|
|
let entries = if attrs.peek().is_none() {
|
|
|
|
// Avoid heap allocation
|
|
|
|
None
|
|
|
|
} else {
|
2020-05-30 18:21:06 +00:00
|
|
|
Some(attrs.flat_map(|ast| Attr::from_src(ast, hygiene)).chain(docs).collect())
|
2019-11-24 12:50:45 +00:00
|
|
|
};
|
|
|
|
Attrs { entries }
|
|
|
|
}
|
|
|
|
|
2020-06-23 16:20:51 +00:00
|
|
|
pub fn merge(&self, other: Attrs) -> Attrs {
|
|
|
|
match (&self.entries, &other.entries) {
|
|
|
|
(None, None) => Attrs { entries: None },
|
|
|
|
(Some(entries), None) | (None, Some(entries)) => {
|
|
|
|
Attrs { entries: Some(entries.clone()) }
|
|
|
|
}
|
|
|
|
(Some(a), Some(b)) => {
|
|
|
|
Attrs { entries: Some(a.iter().chain(b.iter()).cloned().collect()) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-24 13:03:02 +00:00
|
|
|
pub fn by_key(&self, key: &'static str) -> AttrQuery<'_> {
|
|
|
|
AttrQuery { attrs: self, key }
|
2019-11-23 09:01:56 +00:00
|
|
|
}
|
2020-04-09 16:32:02 +00:00
|
|
|
|
2020-07-23 14:22:17 +00:00
|
|
|
pub fn cfg(&self) -> impl Iterator<Item = CfgExpr> + '_ {
|
2020-04-11 15:18:42 +00:00
|
|
|
// FIXME: handle cfg_attr :-)
|
2020-07-23 14:22:17 +00:00
|
|
|
self.by_key("cfg").tt_values().map(CfgExpr::parse)
|
|
|
|
}
|
|
|
|
pub(crate) fn is_cfg_enabled(&self, cfg_options: &CfgOptions) -> bool {
|
|
|
|
self.cfg().all(|cfg| cfg_options.check(&cfg) != Some(false))
|
2020-04-09 16:32:02 +00:00
|
|
|
}
|
2019-11-22 08:27:47 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 13:12:55 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct Attr {
|
2019-12-13 11:12:36 +00:00
|
|
|
pub(crate) path: ModPath,
|
2019-10-30 13:12:55 +00:00
|
|
|
pub(crate) input: Option<AttrInput>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub enum AttrInput {
|
2020-04-25 14:24:44 +00:00
|
|
|
/// `#[attr = "string"]`
|
2019-10-30 13:12:55 +00:00
|
|
|
Literal(SmolStr),
|
2020-04-25 14:24:44 +00:00
|
|
|
/// `#[attr(subtree)]`
|
2019-10-30 13:12:55 +00:00
|
|
|
TokenTree(Subtree),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Attr {
|
2019-11-24 12:50:45 +00:00
|
|
|
fn from_src(ast: ast::Attr, hygiene: &Hygiene) -> Option<Attr> {
|
2019-12-13 11:12:36 +00:00
|
|
|
let path = ModPath::from_src(ast.path()?, hygiene)?;
|
2020-07-30 18:16:04 +00:00
|
|
|
let input = if let Some(lit) = ast.literal() {
|
|
|
|
// FIXME: escape? raw string?
|
|
|
|
let value = lit.syntax().first_token()?.text().trim_matches('"').into();
|
|
|
|
Some(AttrInput::Literal(value))
|
|
|
|
} else if let Some(tt) = ast.token_tree() {
|
|
|
|
Some(AttrInput::TokenTree(ast_to_token_tree(&tt)?.0))
|
|
|
|
} else {
|
|
|
|
None
|
2019-10-30 13:12:55 +00:00
|
|
|
};
|
|
|
|
Some(Attr { path, input })
|
|
|
|
}
|
2019-11-24 13:03:02 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 12:58:24 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2019-11-24 13:03:02 +00:00
|
|
|
pub struct AttrQuery<'a> {
|
|
|
|
attrs: &'a Attrs,
|
|
|
|
key: &'static str,
|
|
|
|
}
|
2019-10-30 13:12:55 +00:00
|
|
|
|
2019-11-24 13:03:02 +00:00
|
|
|
impl<'a> AttrQuery<'a> {
|
|
|
|
pub fn tt_values(self) -> impl Iterator<Item = &'a Subtree> {
|
|
|
|
self.attrs().filter_map(|attr| match attr.input.as_ref()? {
|
|
|
|
AttrInput::TokenTree(it) => Some(it),
|
|
|
|
_ => None,
|
|
|
|
})
|
2019-10-30 13:12:55 +00:00
|
|
|
}
|
|
|
|
|
2019-11-24 13:03:02 +00:00
|
|
|
pub fn string_value(self) -> Option<&'a SmolStr> {
|
|
|
|
self.attrs().find_map(|attr| match attr.input.as_ref()? {
|
|
|
|
AttrInput::Literal(it) => Some(it),
|
2019-10-30 13:12:55 +00:00
|
|
|
_ => None,
|
2019-11-24 13:03:02 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn exists(self) -> bool {
|
|
|
|
self.attrs().next().is_some()
|
2019-10-30 13:12:55 +00:00
|
|
|
}
|
|
|
|
|
2019-11-24 13:03:02 +00:00
|
|
|
fn attrs(self) -> impl Iterator<Item = &'a Attr> {
|
|
|
|
let key = self.key;
|
|
|
|
self.attrs
|
|
|
|
.iter()
|
|
|
|
.filter(move |attr| attr.path.as_ident().map_or(false, |s| s.to_string() == key))
|
2019-10-30 13:12:55 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-23 08:14:10 +00:00
|
|
|
|
2020-03-13 15:05:46 +00:00
|
|
|
fn attrs_from_ast<N>(src: AstId<N>, db: &dyn DefDatabase) -> Attrs
|
2019-11-23 08:14:10 +00:00
|
|
|
where
|
|
|
|
N: ast::AttrsOwner,
|
|
|
|
{
|
2020-03-13 15:05:46 +00:00
|
|
|
let src = InFile::new(src.file_id, src.to_node(db.upcast()));
|
2019-11-24 12:50:45 +00:00
|
|
|
Attrs::from_attrs_owner(db, src.as_ref().map(|it| it as &dyn AttrsOwner))
|
2019-11-23 08:14:10 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 17:15:54 +00:00
|
|
|
fn attrs_from_item_tree<N: ItemTreeNode>(id: ItemTreeId<N>, db: &dyn DefDatabase) -> Attrs {
|
|
|
|
let tree = db.item_tree(id.file_id);
|
|
|
|
let mod_item = N::id_to_mod_item(id.value);
|
2020-06-25 12:39:27 +00:00
|
|
|
tree.attrs(mod_item.into()).clone()
|
2019-11-23 08:14:10 +00:00
|
|
|
}
|