|
1 | 1 | //! Post-nameres attribute resolution.
|
2 | 2 |
|
3 |
| -use hir_expand::{attrs::Attr, MacroCallId}; |
| 3 | +use base_db::CrateId; |
| 4 | +use hir_expand::{ |
| 5 | + attrs::{Attr, AttrId, AttrInput}, |
| 6 | + MacroCallId, MacroCallKind, MacroDefId, |
| 7 | +}; |
| 8 | +use span::Span; |
4 | 9 | use syntax::{ast, SmolStr};
|
| 10 | +use triomphe::Arc; |
5 | 11 |
|
6 | 12 | use crate::{
|
7 | 13 | attr::builtin::{find_builtin_attr_idx, TOOL_MODULES},
|
8 |
| - attr_macro_as_call_id, |
9 | 14 | db::DefDatabase,
|
10 | 15 | item_scope::BuiltinShadowMode,
|
11 | 16 | nameres::path_resolution::ResolveMode,
|
12 |
| - path::{ModPath, PathKind}, |
13 |
| - AstIdWithPath, LocalModuleId, UnresolvedMacro, |
| 17 | + path::{self, ModPath, PathKind}, |
| 18 | + AstIdWithPath, LocalModuleId, MacroId, UnresolvedMacro, |
14 | 19 | };
|
15 | 20 |
|
16 | 21 | use super::{DefMap, MacroSubNs};
|
@@ -93,3 +98,57 @@ impl DefMap {
|
93 | 98 | false
|
94 | 99 | }
|
95 | 100 | }
|
| 101 | + |
| 102 | +pub(super) fn attr_macro_as_call_id( |
| 103 | + db: &dyn DefDatabase, |
| 104 | + item_attr: &AstIdWithPath<ast::Item>, |
| 105 | + macro_attr: &Attr, |
| 106 | + krate: CrateId, |
| 107 | + def: MacroDefId, |
| 108 | +) -> MacroCallId { |
| 109 | + let arg = match macro_attr.input.as_deref() { |
| 110 | + Some(AttrInput::TokenTree(tt)) => { |
| 111 | + let mut tt = tt.as_ref().clone(); |
| 112 | + tt.delimiter = tt::Delimiter::invisible_spanned(macro_attr.span); |
| 113 | + Some(tt) |
| 114 | + } |
| 115 | + |
| 116 | + _ => None, |
| 117 | + }; |
| 118 | + |
| 119 | + def.as_lazy_macro( |
| 120 | + db.upcast(), |
| 121 | + krate, |
| 122 | + MacroCallKind::Attr { |
| 123 | + ast_id: item_attr.ast_id, |
| 124 | + attr_args: arg.map(Arc::new), |
| 125 | + invoc_attr_index: macro_attr.id, |
| 126 | + }, |
| 127 | + macro_attr.span, |
| 128 | + ) |
| 129 | +} |
| 130 | + |
| 131 | +pub(super) fn derive_macro_as_call_id( |
| 132 | + db: &dyn DefDatabase, |
| 133 | + item_attr: &AstIdWithPath<ast::Adt>, |
| 134 | + derive_attr_index: AttrId, |
| 135 | + derive_pos: u32, |
| 136 | + call_site: Span, |
| 137 | + krate: CrateId, |
| 138 | + resolver: impl Fn(path::ModPath) -> Option<(MacroId, MacroDefId)>, |
| 139 | +) -> Result<(MacroId, MacroDefId, MacroCallId), UnresolvedMacro> { |
| 140 | + let (macro_id, def_id) = resolver(item_attr.path.clone()) |
| 141 | + .filter(|(_, def_id)| def_id.is_derive()) |
| 142 | + .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?; |
| 143 | + let call_id = def_id.as_lazy_macro( |
| 144 | + db.upcast(), |
| 145 | + krate, |
| 146 | + MacroCallKind::Derive { |
| 147 | + ast_id: item_attr.ast_id, |
| 148 | + derive_index: derive_pos, |
| 149 | + derive_attr_index, |
| 150 | + }, |
| 151 | + call_site, |
| 152 | + ); |
| 153 | + Ok((macro_id, def_id, call_id)) |
| 154 | +} |
0 commit comments