From e5a2c6596ddd11b0d57042224ac7c1d7691ec33b Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 31 May 2021 13:37:11 +0200 Subject: Expand procedural attribute macros --- crates/hir_def/src/lib.rs | 40 +++++++++++++++++++++++ crates/hir_def/src/nameres/collector.rs | 56 +++++++++++++++++++++++++++++---- 2 files changed, 90 insertions(+), 6 deletions(-) (limited to 'crates/hir_def') diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs index 9aa95720a..987485acc 100644 --- a/crates/hir_def/src/lib.rs +++ b/crates/hir_def/src/lib.rs @@ -55,6 +55,7 @@ use std::{ sync::Arc, }; +use attr::Attr; use base_db::{impl_intern_key, salsa, CrateId}; use hir_expand::{ ast_id_map::FileAstId, @@ -768,3 +769,42 @@ fn derive_macro_as_call_id( .into(); Ok(res) } + +fn attr_macro_as_call_id( + item_attr: &AstIdWithPath, + macro_attr: &Attr, + db: &dyn db::DefDatabase, + krate: CrateId, + resolver: impl Fn(path::ModPath) -> Option, +) -> Result { + let def: MacroDefId = resolver(item_attr.path.clone()) + .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?; + let last_segment = item_attr + .path + .segments() + .last() + .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?; + let mut arg = match ¯o_attr.input { + Some(input) => match &**input { + attr::AttrInput::Literal(_) => tt::Subtree::default(), + attr::AttrInput::TokenTree(tt) => tt.clone(), + }, + None => tt::Subtree::default(), + }; + // The parentheses are always disposed here. + arg.delimiter = None; + + let res = def + .as_lazy_macro( + db.upcast(), + krate, + MacroCallKind::Attr { + ast_id: item_attr.ast_id, + attr_name: last_segment.to_string(), + attr_args: arg, + invoc_attr_index: macro_attr.id.ast_index, + }, + ) + .into(); + Ok(res) +} diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index 6b41921ae..874a4ebb1 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs @@ -23,7 +23,7 @@ use syntax::ast; use crate::{ attr::{Attr, AttrId, AttrInput, Attrs}, - builtin_attr, + attr_macro_as_call_id, builtin_attr, db::DefDatabase, derive_macro_as_call_id, intern::Interned, @@ -223,7 +223,7 @@ struct MacroDirective { enum MacroDirectiveKind { FnLike { ast_id: AstIdWithPath, fragment: FragmentKind }, Derive { ast_id: AstIdWithPath, derive_attr: AttrId }, - Attr { ast_id: AstIdWithPath, attr: AttrId, mod_item: ModItem }, + Attr { ast_id: AstIdWithPath, attr: Attr, mod_item: ModItem }, } struct DefData<'a> { @@ -419,7 +419,7 @@ impl DefCollector<'_> { let mut unresolved_macros = std::mem::replace(&mut self.unresolved_macros, Vec::new()); let pos = unresolved_macros.iter().position(|directive| { if let MacroDirectiveKind::Attr { ast_id, mod_item, attr } = &directive.kind { - self.skip_attrs.insert(ast_id.ast_id.with_value(*mod_item), *attr); + self.skip_attrs.insert(ast_id.ast_id.with_value(*mod_item), attr.id); let file_id = ast_id.ast_id.file_id; let item_tree = self.db.file_item_tree(file_id); @@ -1050,7 +1050,7 @@ impl DefCollector<'_> { let file_id = ast_id.ast_id.file_id; let item_tree = self.db.file_item_tree(file_id); let mod_dir = self.mod_dirs[&directive.module_id].clone(); - self.skip_attrs.insert(InFile::new(file_id, *mod_item), *attr); + self.skip_attrs.insert(InFile::new(file_id, *mod_item), attr.id); ModCollector { def_collector: &mut *self, macro_depth: directive.depth, @@ -1068,7 +1068,51 @@ impl DefCollector<'_> { } // Not resolved to a derive helper, so try to resolve as a macro. - // FIXME: not yet :) + match attr_macro_as_call_id( + ast_id, + attr, + self.db, + self.def_map.krate, + &resolver, + ) { + Ok(call_id) => { + let loc: MacroCallLoc = self.db.lookup_intern_macro(call_id); + if let MacroDefKind::ProcMacro(exp, ..) = &loc.def.kind { + if exp.is_dummy() { + // Proc macros that cannot be expanded are treated as not + // resolved, in order to fall back later. + self.def_map.diagnostics.push( + DefDiagnostic::unresolved_proc_macro( + directive.module_id, + loc.kind, + ), + ); + + let file_id = ast_id.ast_id.file_id; + let item_tree = self.db.file_item_tree(file_id); + let mod_dir = self.mod_dirs[&directive.module_id].clone(); + self.skip_attrs + .insert(InFile::new(file_id, *mod_item), attr.id); + ModCollector { + def_collector: &mut *self, + macro_depth: directive.depth, + module_id: directive.module_id, + file_id, + item_tree: &item_tree, + mod_dir, + } + .collect(&[*mod_item]); + + // Remove the macro directive. + return false; + } + } + resolved.push((directive.module_id, call_id, directive.depth)); + res = ReachedFixedPoint::No; + return false; + } + Err(UnresolvedMacro { .. }) => (), + } } } @@ -1628,7 +1672,7 @@ impl ModCollector<'_, '_> { self.def_collector.unresolved_macros.push(MacroDirective { module_id: self.module_id, depth: self.macro_depth + 1, - kind: MacroDirectiveKind::Attr { ast_id, attr: attr.id, mod_item }, + kind: MacroDirectiveKind::Attr { ast_id, attr: attr.clone(), mod_item }, }); return Err(()); -- cgit v1.2.3