aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_def/src/lib.rs
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2021-05-31 12:37:11 +0100
committerJonas Schievink <[email protected]>2021-06-03 17:09:21 +0100
commite5a2c6596ddd11b0d57042224ac7c1d7691ec33b (patch)
treef0476ad40103b5d3dea60f81fca32c63fe9618d7 /crates/hir_def/src/lib.rs
parent7f9c4a59d9a84cd8c734286937439b5cd215be27 (diff)
Expand procedural attribute macros
Diffstat (limited to 'crates/hir_def/src/lib.rs')
-rw-r--r--crates/hir_def/src/lib.rs40
1 files changed, 40 insertions, 0 deletions
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::{
55 sync::Arc, 55 sync::Arc,
56}; 56};
57 57
58use attr::Attr;
58use base_db::{impl_intern_key, salsa, CrateId}; 59use base_db::{impl_intern_key, salsa, CrateId};
59use hir_expand::{ 60use hir_expand::{
60 ast_id_map::FileAstId, 61 ast_id_map::FileAstId,
@@ -768,3 +769,42 @@ fn derive_macro_as_call_id(
768 .into(); 769 .into();
769 Ok(res) 770 Ok(res)
770} 771}
772
773fn attr_macro_as_call_id(
774 item_attr: &AstIdWithPath<ast::Item>,
775 macro_attr: &Attr,
776 db: &dyn db::DefDatabase,
777 krate: CrateId,
778 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
779) -> Result<MacroCallId, UnresolvedMacro> {
780 let def: MacroDefId = resolver(item_attr.path.clone())
781 .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
782 let last_segment = item_attr
783 .path
784 .segments()
785 .last()
786 .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
787 let mut arg = match &macro_attr.input {
788 Some(input) => match &**input {
789 attr::AttrInput::Literal(_) => tt::Subtree::default(),
790 attr::AttrInput::TokenTree(tt) => tt.clone(),
791 },
792 None => tt::Subtree::default(),
793 };
794 // The parentheses are always disposed here.
795 arg.delimiter = None;
796
797 let res = def
798 .as_lazy_macro(
799 db.upcast(),
800 krate,
801 MacroCallKind::Attr {
802 ast_id: item_attr.ast_id,
803 attr_name: last_segment.to_string(),
804 attr_args: arg,
805 invoc_attr_index: macro_attr.id.ast_index,
806 },
807 )
808 .into();
809 Ok(res)
810}