rust-analyzer/crates/ra_hir/src/from_source.rs
bors[bot] 6e10a9f578
Merge #2479
2479: Add expansion infrastructure for derive macros r=matklad a=flodiebold

I thought I'd experiment a bit with attribute macro/derive expansion, and here's what I've got so far. It has dummy implementations of the Copy / Clone derives, to show that the approach works; it doesn't add any attribute macro support, but I think that fits into the architecture.

Basically, during raw item collection, we look at the attributes and generate macro calls for them if necessary. Currently I only do this for derives, and just add the derive macro calls as separate calls next to the item. I think for derives, it's important that they don't obscure the actual item, since they can't actually change it (e.g. sending the item token tree through macro expansion unnecessarily might make completion within it more complicated).

Attribute macros would have to be recognized at that stage and replace the item (i.e., the raw item collector will just emit an attribute macro call, and not the item). I think when we implement this, we should try to recognize known inert attributes, so that we don't do macro expansion unnecessarily; anything that isn't known needs to be treated as a possible attribute macro call (since the raw item collector can't resolve the macro yet).

There's basically no name resolution for attribute macros implemented, I just hardcoded the built-in derives. In the future, the built-ins should work within the normal name resolution infrastructure; the problem there is that the builtin stubs in `std` use macros 2.0, which we don't support yet (and adding support is outside the scope of this).

One aspect that I don't really have a solution for, but I don't know how important it is, is removing the attribute itself from its input. I'm pretty sure rustc leaves out the attribute macro from the input, but to do that, we'd have to create a completely new syntax node. I guess we could do it when / after converting to a token tree.

Co-authored-by: Florian Diebold <flodiebold@gmail.com>
2019-12-05 20:00:20 +00:00

275 lines
10 KiB
Rust

//! FIXME: write short doc here
use either::Either;
use hir_def::{
child_from_source::ChildFromSource, nameres::ModuleSource, AstItemDef, EnumVariantId, ImplId,
LocationCtx, ModuleId, TraitId, VariantId,
};
use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind};
use ra_syntax::{
ast::{self, AstNode, NameOwner},
match_ast, SyntaxNode,
};
use crate::{
db::{AstDatabase, DefDatabase, HirDatabase},
Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, ImplBlock, InFile, Local,
MacroDef, Module, Static, Struct, StructField, Trait, TypeAlias, Union,
};
pub trait FromSource: Sized {
type Ast;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self>;
}
impl FromSource for Struct {
type Ast = ast::StructDef;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
let id = from_source(db, src)?;
Some(Struct { id })
}
}
impl FromSource for Union {
type Ast = ast::UnionDef;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
let id = from_source(db, src)?;
Some(Union { id })
}
}
impl FromSource for Enum {
type Ast = ast::EnumDef;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
let id = from_source(db, src)?;
Some(Enum { id })
}
}
impl FromSource for Trait {
type Ast = ast::TraitDef;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
let id = from_source(db, src)?;
Some(Trait { id })
}
}
impl FromSource for Function {
type Ast = ast::FnDef;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
Container::find(db, src.as_ref().map(|it| it.syntax()))?
.child_from_source(db, src)
.map(Function::from)
}
}
impl FromSource for Const {
type Ast = ast::ConstDef;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
Container::find(db, src.as_ref().map(|it| it.syntax()))?
.child_from_source(db, src)
.map(Const::from)
}
}
impl FromSource for Static {
type Ast = ast::StaticDef;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
match Container::find(db, src.as_ref().map(|it| it.syntax()))? {
Container::Module(it) => it.id.child_from_source(db, src).map(Static::from),
Container::Trait(_) | Container::ImplBlock(_) => None,
}
}
}
impl FromSource for TypeAlias {
type Ast = ast::TypeAliasDef;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
Container::find(db, src.as_ref().map(|it| it.syntax()))?
.child_from_source(db, src)
.map(TypeAlias::from)
}
}
impl FromSource for MacroDef {
type Ast = ast::MacroCall;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
let kind = MacroDefKind::Declarative;
let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax()));
let module = Module::from_definition(db, InFile::new(src.file_id, module_src))?;
let krate = Some(module.krate().crate_id());
let ast_id = Some(AstId::new(src.file_id, db.ast_id_map(src.file_id).ast_id(&src.value)));
let id: MacroDefId = MacroDefId { krate, ast_id, kind };
Some(MacroDef { id })
}
}
impl FromSource for ImplBlock {
type Ast = ast::ImplBlock;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
let id = from_source(db, src)?;
Some(ImplBlock { id })
}
}
impl FromSource for EnumVariant {
type Ast = ast::EnumVariant;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
let parent_enum = src.value.parent_enum();
let src_enum = InFile { file_id: src.file_id, value: parent_enum };
let parent_enum = Enum::from_source(db, src_enum)?;
parent_enum.id.child_from_source(db, src).map(EnumVariant::from)
}
}
impl FromSource for StructField {
type Ast = FieldSource;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
let variant_id: VariantId = match src.value {
FieldSource::Named(ref field) => {
let value = field.syntax().ancestors().find_map(ast::StructDef::cast)?;
let src = InFile { file_id: src.file_id, value };
let def = Struct::from_source(db, src)?;
def.id.into()
}
FieldSource::Pos(ref field) => {
let value = field.syntax().ancestors().find_map(ast::EnumVariant::cast)?;
let src = InFile { file_id: src.file_id, value };
let def = EnumVariant::from_source(db, src)?;
EnumVariantId::from(def).into()
}
};
let src = src.map(|field_source| match field_source {
FieldSource::Pos(it) => Either::Left(it),
FieldSource::Named(it) => Either::Right(it),
});
variant_id.child_from_source(db, src).map(StructField::from)
}
}
impl Local {
pub fn from_source(db: &impl HirDatabase, src: InFile<ast::BindPat>) -> Option<Self> {
let file_id = src.file_id;
let parent: DefWithBody = src.value.syntax().ancestors().find_map(|it| {
let res = match_ast! {
match it {
ast::ConstDef(value) => { Const::from_source(db, InFile { value, file_id})?.into() },
ast::StaticDef(value) => { Static::from_source(db, InFile { value, file_id})?.into() },
ast::FnDef(value) => { Function::from_source(db, InFile { value, file_id})?.into() },
_ => return None,
}
};
Some(res)
})?;
let (_body, source_map) = db.body_with_source_map(parent.into());
let src = src.map(ast::Pat::from);
let pat_id = source_map.node_pat(src.as_ref())?;
Some(Local { parent, pat_id })
}
}
impl Module {
pub fn from_declaration(db: &impl DefDatabase, src: InFile<ast::Module>) -> Option<Self> {
let parent_declaration = src.value.syntax().ancestors().skip(1).find_map(ast::Module::cast);
let parent_module = match parent_declaration {
Some(parent_declaration) => {
let src_parent = InFile { file_id: src.file_id, value: parent_declaration };
Module::from_declaration(db, src_parent)
}
None => {
let source_file = db.parse(src.file_id.original_file(db)).tree();
let src_parent =
InFile { file_id: src.file_id, value: ModuleSource::SourceFile(source_file) };
Module::from_definition(db, src_parent)
}
}?;
let child_name = src.value.name()?;
parent_module.child(db, &child_name.as_name())
}
pub fn from_definition(db: &impl DefDatabase, src: InFile<ModuleSource>) -> Option<Self> {
match src.value {
ModuleSource::Module(ref module) => {
assert!(!module.has_semi());
return Module::from_declaration(
db,
InFile { file_id: src.file_id, value: module.clone() },
);
}
ModuleSource::SourceFile(_) => (),
};
let original_file = src.file_id.original_file(db);
let (krate, local_id) = db.relevant_crates(original_file).iter().find_map(|&crate_id| {
let crate_def_map = db.crate_def_map(crate_id);
let local_id = crate_def_map.modules_for_file(original_file).next()?;
Some((crate_id, local_id))
})?;
Some(Module { id: ModuleId { krate, local_id } })
}
}
fn from_source<N, DEF>(db: &(impl DefDatabase + AstDatabase), src: InFile<N>) -> Option<DEF>
where
N: AstNode,
DEF: AstItemDef<N>,
{
let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax()));
let module = Module::from_definition(db, InFile::new(src.file_id, module_src))?;
let ctx = LocationCtx::new(db, module.id, src.file_id);
let items = db.ast_id_map(src.file_id);
let item_id = items.ast_id(&src.value);
Some(DEF::from_ast_id(ctx, item_id))
}
enum Container {
Trait(Trait),
ImplBlock(ImplBlock),
Module(Module),
}
impl Container {
fn find(db: &impl DefDatabase, src: InFile<&SyntaxNode>) -> Option<Container> {
// FIXME: this doesn't try to handle nested declarations
for container in src.value.ancestors() {
let res = match_ast! {
match container {
ast::TraitDef(it) => {
let c = Trait::from_source(db, src.with_value(it))?;
Container::Trait(c)
},
ast::ImplBlock(it) => {
let c = ImplBlock::from_source(db, src.with_value(it))?;
Container::ImplBlock(c)
},
_ => { continue },
}
};
return Some(res);
}
let module_source = ModuleSource::from_child_node(db, src);
let c = Module::from_definition(db, src.with_value(module_source))?;
Some(Container::Module(c))
}
}
impl<CHILD, SOURCE> ChildFromSource<CHILD, SOURCE> for Container
where
TraitId: ChildFromSource<CHILD, SOURCE>,
ImplId: ChildFromSource<CHILD, SOURCE>,
ModuleId: ChildFromSource<CHILD, SOURCE>,
{
fn child_from_source(
&self,
db: &impl DefDatabase,
child_source: InFile<SOURCE>,
) -> Option<CHILD> {
match self {
Container::Trait(it) => it.id.child_from_source(db, child_source),
Container::ImplBlock(it) => it.id.child_from_source(db, child_source),
Container::Module(it) => it.id.child_from_source(db, child_source),
}
}
}