2019-10-30 08:12:55 -05:00
|
|
|
//! A higher level attributes based on TokenTree, with also some shortcuts.
|
|
|
|
|
2019-11-22 02:27:47 -06:00
|
|
|
use std::{ops, sync::Arc};
|
2019-10-30 08:12:55 -05:00
|
|
|
|
2019-11-23 02:14:10 -06:00
|
|
|
use hir_expand::{either::Either, hygiene::Hygiene, AstId};
|
2019-10-30 08:12:55 -05:00
|
|
|
use mbe::ast_to_token_tree;
|
|
|
|
use ra_cfg::CfgOptions;
|
|
|
|
use ra_syntax::{
|
|
|
|
ast::{self, AstNode, AttrsOwner},
|
|
|
|
SmolStr,
|
|
|
|
};
|
|
|
|
use tt::Subtree;
|
|
|
|
|
2019-11-23 02:14:10 -06:00
|
|
|
use crate::{
|
2019-11-23 05:44:43 -06:00
|
|
|
db::DefDatabase, path::Path, AdtId, AstItemDef, AttrDefId, HasChildSource, HasSource, Lookup,
|
2019-11-23 02:14:10 -06:00
|
|
|
};
|
2019-10-30 08:12:55 -05:00
|
|
|
|
2019-11-22 02:27:47 -06: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 {
|
2019-11-23 05:44:43 -06:00
|
|
|
pub(crate) fn attrs_query(db: &impl DefDatabase, def: AttrDefId) -> Attrs {
|
2019-11-23 02:14:10 -06:00
|
|
|
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::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)
|
|
|
|
}
|
2019-11-23 03:01:56 -06:00
|
|
|
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::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),
|
2019-11-23 02:14:10 -06:00
|
|
|
AttrDefId::ConstId(it) => attrs_from_loc(it.lookup(db), db),
|
2019-11-24 06:13:56 -06:00
|
|
|
AttrDefId::StaticId(it) => attrs_from_loc(it.lookup(db), db),
|
2019-11-23 02:14:10 -06:00
|
|
|
AttrDefId::FunctionId(it) => attrs_from_loc(it.lookup(db), db),
|
|
|
|
AttrDefId::TypeAliasId(it) => attrs_from_loc(it.lookup(db), db),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 02:27:47 -06:00
|
|
|
pub fn has_atom(&self, atom: &str) -> bool {
|
|
|
|
self.iter().any(|it| it.is_simple_atom(atom))
|
|
|
|
}
|
2019-11-23 03:01:56 -06:00
|
|
|
|
|
|
|
pub fn find_string_value(&self, key: &str) -> Option<SmolStr> {
|
|
|
|
self.iter().filter(|attr| attr.is_simple_atom(key)).find_map(|attr| {
|
|
|
|
match attr.input.as_ref()? {
|
|
|
|
AttrInput::Literal(it) => Some(it.clone()),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-11-22 02:27:47 -06:00
|
|
|
}
|
|
|
|
|
2019-10-30 08:12:55 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct Attr {
|
|
|
|
pub(crate) path: Path,
|
|
|
|
pub(crate) input: Option<AttrInput>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub enum AttrInput {
|
|
|
|
Literal(SmolStr),
|
|
|
|
TokenTree(Subtree),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Attr {
|
2019-10-30 10:41:50 -05:00
|
|
|
pub(crate) fn from_src(ast: ast::Attr, hygiene: &Hygiene) -> Option<Attr> {
|
|
|
|
let path = Path::from_src(ast.path()?, hygiene)?;
|
2019-10-30 08:12:55 -05:00
|
|
|
let input = match ast.input() {
|
|
|
|
None => None,
|
|
|
|
Some(ast::AttrInput::Literal(lit)) => {
|
|
|
|
// FIXME: escape? raw string?
|
|
|
|
let value = lit.syntax().first_token()?.text().trim_matches('"').into();
|
|
|
|
Some(AttrInput::Literal(value))
|
|
|
|
}
|
|
|
|
Some(ast::AttrInput::TokenTree(tt)) => {
|
|
|
|
Some(AttrInput::TokenTree(ast_to_token_tree(&tt)?.0))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(Attr { path, input })
|
|
|
|
}
|
|
|
|
|
2019-11-22 02:27:47 -06:00
|
|
|
pub fn from_attrs_owner(owner: &dyn AttrsOwner, hygiene: &Hygiene) -> Attrs {
|
2019-10-30 08:12:55 -05:00
|
|
|
let mut attrs = owner.attrs().peekable();
|
2019-11-22 02:27:47 -06:00
|
|
|
let entries = if attrs.peek().is_none() {
|
2019-10-30 08:12:55 -05:00
|
|
|
// Avoid heap allocation
|
2019-11-22 02:27:47 -06:00
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(attrs.flat_map(|ast| Attr::from_src(ast, hygiene)).collect())
|
|
|
|
};
|
|
|
|
Attrs { entries }
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_simple_atom(&self, name: &str) -> bool {
|
|
|
|
// FIXME: Avoid cloning
|
|
|
|
self.path.as_ident().map_or(false, |s| s.to_string() == name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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<bool> {
|
|
|
|
cfg_options.is_cfg_enabled(self.as_cfg()?)
|
|
|
|
}
|
|
|
|
}
|
2019-11-23 02:14:10 -06:00
|
|
|
|
|
|
|
fn attrs_from_ast<D, N>(src: AstId<N>, db: &D) -> Attrs
|
|
|
|
where
|
|
|
|
N: ast::AttrsOwner,
|
2019-11-23 05:44:43 -06:00
|
|
|
D: DefDatabase,
|
2019-11-23 02:14:10 -06:00
|
|
|
{
|
|
|
|
let hygiene = Hygiene::new(db, src.file_id());
|
|
|
|
Attr::from_attrs_owner(&src.to_node(db), &hygiene)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn attrs_from_loc<T, D>(node: T, db: &D) -> Attrs
|
|
|
|
where
|
|
|
|
T: HasSource,
|
|
|
|
T::Value: ast::AttrsOwner,
|
2019-11-23 05:44:43 -06:00
|
|
|
D: DefDatabase,
|
2019-11-23 02:14:10 -06:00
|
|
|
{
|
|
|
|
let src = node.source(db);
|
|
|
|
let hygiene = Hygiene::new(db, src.file_id);
|
|
|
|
Attr::from_attrs_owner(&src.value, &hygiene)
|
|
|
|
}
|