1 //! `hir_expand` deals with macro expansion.
3 //! Specifically, it implements a concept of `MacroFile` -- a file whose syntax
4 //! tree originates not from the text of some `FileId`, but from some macro
11 pub mod builtin_attr_macro;
12 pub mod builtin_derive_macro;
13 pub mod builtin_fn_macro;
20 pub use mbe::{Origin, ValueResult};
22 use std::{fmt, hash::Hash, iter, sync::Arc};
24 use base_db::{impl_intern_key, salsa, CrateId, FileId, FileRange, ProcMacroKind};
27 algo::{self, skip_trivia_token},
28 ast::{self, AstNode, HasDocComments},
29 Direction, SyntaxNode, SyntaxToken,
33 ast_id_map::FileAstId,
34 builtin_attr_macro::BuiltinAttrExpander,
35 builtin_derive_macro::BuiltinDeriveExpander,
36 builtin_fn_macro::{BuiltinFnLikeExpander, EagerExpander},
39 proc_macro::ProcMacroExpander,
42 pub type ExpandResult<T> = ValueResult<T, ExpandError>;
44 #[derive(Debug, PartialEq, Eq, Clone)]
45 pub enum ExpandError {
47 Mbe(mbe::ExpandError),
51 impl From<mbe::ExpandError> for ExpandError {
52 fn from(mbe: mbe::ExpandError) -> Self {
57 impl fmt::Display for ExpandError {
58 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60 ExpandError::UnresolvedProcMacro => f.write_str("unresolved proc-macro"),
61 ExpandError::Mbe(it) => it.fmt(f),
62 ExpandError::Other(it) => f.write_str(it),
67 /// Input to the analyzer is a set of files, where each file is identified by
68 /// `FileId` and contains source code. However, another source of source code in
69 /// Rust are macros: each macro can be thought of as producing a "temporary
70 /// file". To assign an id to such a file, we use the id of the macro call that
71 /// produced the file. So, a `HirFileId` is either a `FileId` (source code
72 /// written by user), or a `MacroCallId` (source code produced by macro).
74 /// What is a `MacroCallId`? Simplifying, it's a `HirFileId` of a file
75 /// containing the call plus the offset of the macro call in the file. Note that
76 /// this is a recursive definition! However, the size_of of `HirFileId` is
77 /// finite (because everything bottoms out at the real `FileId`) and small
78 /// (`MacroCallId` uses the location interning. You can check details here:
79 /// <https://en.wikipedia.org/wiki/String_interning>).
80 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
81 pub struct HirFileId(HirFileIdRepr);
83 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
89 impl From<FileId> for HirFileId {
90 fn from(id: FileId) -> Self {
91 HirFileId(HirFileIdRepr::FileId(id))
95 impl From<MacroFile> for HirFileId {
96 fn from(id: MacroFile) -> Self {
97 HirFileId(HirFileIdRepr::MacroFile(id))
101 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
102 pub struct MacroFile {
103 pub macro_call_id: MacroCallId,
106 /// `MacroCallId` identifies a particular macro invocation, like
107 /// `println!("Hello, {}", world)`.
108 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
109 pub struct MacroCallId(salsa::InternId);
110 impl_intern_key!(MacroCallId);
112 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
113 pub struct MacroCallLoc {
115 pub(crate) krate: CrateId,
116 eager: Option<EagerCallInfo>,
117 pub kind: MacroCallKind,
120 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
121 pub struct MacroDefId {
123 pub kind: MacroDefKind,
124 pub local_inner: bool,
127 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
128 pub enum MacroDefKind {
129 Declarative(AstId<ast::Macro>),
130 BuiltIn(BuiltinFnLikeExpander, AstId<ast::Macro>),
131 // FIXME: maybe just Builtin and rename BuiltinFnLikeExpander to BuiltinExpander
132 BuiltInAttr(BuiltinAttrExpander, AstId<ast::Macro>),
133 BuiltInDerive(BuiltinDeriveExpander, AstId<ast::Macro>),
134 BuiltInEager(EagerExpander, AstId<ast::Macro>),
135 ProcMacro(ProcMacroExpander, ProcMacroKind, AstId<ast::Fn>),
138 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
139 struct EagerCallInfo {
140 /// NOTE: This can be *either* the expansion result, *or* the argument to the eager macro!
141 arg_or_expansion: Arc<tt::Subtree>,
142 included_file: Option<FileId>,
145 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
146 pub enum MacroCallKind {
148 ast_id: AstId<ast::MacroCall>,
152 ast_id: AstId<ast::Adt>,
153 /// Syntactical index of the invoking `#[derive]` attribute.
155 /// Outer attributes are counted first, then inner attributes. This does not support
156 /// out-of-line modules, which may have attributes spread across 2 files!
157 derive_attr_index: u32,
158 /// Index of the derive macro in the derive attribute
162 ast_id: AstId<ast::Item>,
163 attr_args: Arc<(tt::Subtree, mbe::TokenMap)>,
164 /// Syntactical index of the invoking `#[attribute]`.
166 /// Outer attributes are counted first, then inner attributes. This does not support
167 /// out-of-line modules, which may have attributes spread across 2 files!
168 invoc_attr_index: u32,
169 /// Whether this attribute is the `#[derive]` attribute.
175 /// For macro-expansion files, returns the file original source file the
176 /// expansion originated from.
177 pub fn original_file(self, db: &dyn db::AstDatabase) -> FileId {
179 HirFileIdRepr::FileId(file_id) => file_id,
180 HirFileIdRepr::MacroFile(macro_file) => {
181 let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id);
182 let file_id = match loc.eager {
183 Some(EagerCallInfo { included_file: Some(file), .. }) => file.into(),
184 _ => loc.kind.file_id(),
186 file_id.original_file(db)
191 pub fn expansion_level(self, db: &dyn db::AstDatabase) -> u32 {
194 while let HirFileIdRepr::MacroFile(macro_file) = curr.0 {
195 let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id);
198 curr = loc.kind.file_id();
203 /// If this is a macro call, returns the syntax node of the call.
204 pub fn call_node(self, db: &dyn db::AstDatabase) -> Option<InFile<SyntaxNode>> {
206 HirFileIdRepr::FileId(_) => None,
207 HirFileIdRepr::MacroFile(macro_file) => {
208 let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id);
209 Some(loc.kind.to_node(db))
214 /// Return expansion information if it is a macro-expansion file
215 pub fn expansion_info(self, db: &dyn db::AstDatabase) -> Option<ExpansionInfo> {
217 HirFileIdRepr::FileId(_) => None,
218 HirFileIdRepr::MacroFile(macro_file) => {
219 let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id);
221 let arg_tt = loc.kind.arg(db)?;
223 let macro_def = db.macro_def(loc.def).ok()?;
224 let (parse, exp_map) = db.parse_macro_expansion(macro_file).value?;
225 let macro_arg = db.macro_arg(macro_file.macro_call_id)?;
227 let def = loc.def.ast_id().left().and_then(|id| {
228 let def_tt = match id.to_node(db) {
229 ast::Macro::MacroRules(mac) => mac.token_tree()?,
230 ast::Macro::MacroDef(_)
231 if matches!(*macro_def, TokenExpander::BuiltinAttr(_)) =>
235 ast::Macro::MacroDef(mac) => mac.body()?,
237 Some(InFile::new(id.file_id, def_tt))
239 let attr_input_or_mac_def = def.or_else(|| match loc.kind {
240 MacroCallKind::Attr { ast_id, invoc_attr_index, .. } => {
243 .doc_comments_and_attrs()
244 .nth(invoc_attr_index as usize)
245 .and_then(Either::left)?
247 Some(InFile::new(ast_id.file_id, tt))
253 expanded: InFile::new(self, parse.syntax_node()),
254 arg: InFile::new(loc.kind.file_id(), arg_tt),
255 attr_input_or_mac_def,
256 macro_arg_shift: mbe::Shift::new(¯o_arg.0),
265 /// Indicate it is macro file generated for builtin derive
266 pub fn is_builtin_derive(&self, db: &dyn db::AstDatabase) -> Option<InFile<ast::Attr>> {
268 HirFileIdRepr::FileId(_) => None,
269 HirFileIdRepr::MacroFile(macro_file) => {
270 let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id);
271 let attr = match loc.def.kind {
272 MacroDefKind::BuiltInDerive(..) => loc.kind.to_node(db),
275 Some(attr.with_value(ast::Attr::cast(attr.value.clone())?))
280 pub fn is_custom_derive(&self, db: &dyn db::AstDatabase) -> bool {
282 HirFileIdRepr::FileId(_) => false,
283 HirFileIdRepr::MacroFile(macro_file) => {
284 let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id);
285 matches!(loc.def.kind, MacroDefKind::ProcMacro(_, ProcMacroKind::CustomDerive, _))
290 /// Return whether this file is an include macro
291 pub fn is_include_macro(&self, db: &dyn db::AstDatabase) -> bool {
293 HirFileIdRepr::MacroFile(macro_file) => {
294 let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id);
295 matches!(loc.eager, Some(EagerCallInfo { included_file: Some(_), .. }))
301 /// Return whether this file is an attr macro
302 pub fn is_attr_macro(&self, db: &dyn db::AstDatabase) -> bool {
304 HirFileIdRepr::MacroFile(macro_file) => {
305 let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id);
306 matches!(loc.kind, MacroCallKind::Attr { .. })
312 /// Return whether this file is the pseudo expansion of the derive attribute.
313 /// See [`crate::builtin_attr_macro::derive_attr_expand`].
314 pub fn is_derive_attr_pseudo_expansion(&self, db: &dyn db::AstDatabase) -> bool {
316 HirFileIdRepr::MacroFile(macro_file) => {
317 let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id);
318 matches!(loc.kind, MacroCallKind::Attr { is_derive: true, .. })
324 pub fn is_macro(self) -> bool {
325 matches!(self.0, HirFileIdRepr::MacroFile(_))
328 pub fn macro_file(self) -> Option<MacroFile> {
330 HirFileIdRepr::FileId(_) => None,
331 HirFileIdRepr::MacroFile(m) => Some(m),
337 pub fn as_lazy_macro(
339 db: &dyn db::AstDatabase,
343 db.intern_macro_call(MacroCallLoc { def: self, krate, eager: None, kind })
346 pub fn ast_id(&self) -> Either<AstId<ast::Macro>, AstId<ast::Fn>> {
347 let id = match self.kind {
348 MacroDefKind::ProcMacro(.., id) => return Either::Right(id),
349 MacroDefKind::Declarative(id)
350 | MacroDefKind::BuiltIn(_, id)
351 | MacroDefKind::BuiltInAttr(_, id)
352 | MacroDefKind::BuiltInDerive(_, id)
353 | MacroDefKind::BuiltInEager(_, id) => id,
358 pub fn is_proc_macro(&self) -> bool {
359 matches!(self.kind, MacroDefKind::ProcMacro(..))
362 pub fn is_attribute(&self) -> bool {
365 MacroDefKind::BuiltInAttr(..) | MacroDefKind::ProcMacro(_, ProcMacroKind::Attr, _)
370 // FIXME: attribute indices do not account for `cfg_attr`, which means that we'll strip the whole
371 // `cfg_attr` instead of just one of the attributes it expands to
374 /// Returns the file containing the macro invocation.
375 fn file_id(&self) -> HirFileId {
377 MacroCallKind::FnLike { ast_id: InFile { file_id, .. }, .. }
378 | MacroCallKind::Derive { ast_id: InFile { file_id, .. }, .. }
379 | MacroCallKind::Attr { ast_id: InFile { file_id, .. }, .. } => file_id,
383 pub fn to_node(&self, db: &dyn db::AstDatabase) -> InFile<SyntaxNode> {
385 MacroCallKind::FnLike { ast_id, .. } => {
386 ast_id.with_value(ast_id.to_node(db).syntax().clone())
388 MacroCallKind::Derive { ast_id, derive_attr_index, .. } => {
389 // FIXME: handle `cfg_attr`
390 ast_id.with_value(ast_id.to_node(db)).map(|it| {
391 it.doc_comments_and_attrs()
392 .nth(*derive_attr_index as usize)
393 .and_then(|it| match it {
394 Either::Left(attr) => Some(attr.syntax().clone()),
395 Either::Right(_) => None,
397 .unwrap_or_else(|| it.syntax().clone())
400 MacroCallKind::Attr { ast_id, is_derive: true, invoc_attr_index, .. } => {
401 // FIXME: handle `cfg_attr`
402 ast_id.with_value(ast_id.to_node(db)).map(|it| {
403 it.doc_comments_and_attrs()
404 .nth(*invoc_attr_index as usize)
405 .and_then(|it| match it {
406 Either::Left(attr) => Some(attr.syntax().clone()),
407 Either::Right(_) => None,
409 .unwrap_or_else(|| it.syntax().clone())
412 MacroCallKind::Attr { ast_id, .. } => {
413 ast_id.with_value(ast_id.to_node(db).syntax().clone())
418 /// Returns the original file range that best describes the location of this macro call.
420 /// Here we try to roughly match what rustc does to improve diagnostics: fn-like macros
421 /// get the whole `ast::MacroCall`, attribute macros get the attribute's range, and derives
422 /// get only the specific derive that is being referred to.
423 pub fn original_call_range(self, db: &dyn db::AstDatabase) -> FileRange {
426 match kind.file_id().0 {
427 HirFileIdRepr::MacroFile(file) => {
428 kind = db.lookup_intern_macro_call(file.macro_call_id).kind;
430 HirFileIdRepr::FileId(file_id) => break file_id,
434 let range = match kind {
435 MacroCallKind::FnLike { ast_id, .. } => ast_id.to_node(db).syntax().text_range(),
436 MacroCallKind::Derive { ast_id, derive_attr_index, .. } => {
437 // FIXME: should be the range of the macro name, not the whole derive
440 .doc_comments_and_attrs()
441 .nth(derive_attr_index as usize)
442 .expect("missing derive")
443 .expect_left("derive is a doc comment?")
447 MacroCallKind::Attr { ast_id, invoc_attr_index, .. } => ast_id
449 .doc_comments_and_attrs()
450 .nth(invoc_attr_index as usize)
451 .expect("missing attribute")
452 .expect_left("attribute macro is a doc comment?")
457 FileRange { range, file_id }
460 fn arg(&self, db: &dyn db::AstDatabase) -> Option<SyntaxNode> {
462 MacroCallKind::FnLike { ast_id, .. } => {
463 Some(ast_id.to_node(db).token_tree()?.syntax().clone())
465 MacroCallKind::Derive { ast_id, .. } => Some(ast_id.to_node(db).syntax().clone()),
466 MacroCallKind::Attr { ast_id, .. } => Some(ast_id.to_node(db).syntax().clone()),
470 fn expand_to(&self) -> ExpandTo {
472 MacroCallKind::FnLike { expand_to, .. } => *expand_to,
473 MacroCallKind::Derive { .. } => ExpandTo::Items,
474 MacroCallKind::Attr { is_derive: true, .. } => ExpandTo::Statements,
475 MacroCallKind::Attr { .. } => ExpandTo::Items, // is this always correct?
481 pub fn as_file(self) -> HirFileId {
482 MacroFile { macro_call_id: self }.into()
486 /// ExpansionInfo mainly describes how to map text range between src and expanded macro
487 #[derive(Debug, Clone, PartialEq, Eq)]
488 pub struct ExpansionInfo {
489 expanded: InFile<SyntaxNode>,
490 /// The argument TokenTree or item for attributes
491 arg: InFile<SyntaxNode>,
492 /// The `macro_rules!` or attribute input.
493 attr_input_or_mac_def: Option<InFile<ast::TokenTree>>,
495 macro_def: Arc<TokenExpander>,
496 macro_arg: Arc<(tt::Subtree, mbe::TokenMap, fixup::SyntaxFixupUndoInfo)>,
497 /// A shift built from `macro_arg`'s subtree, relevant for attributes as the item is the macro arg
498 /// and as such we need to shift tokens if they are part of an attributes input instead of their item.
499 macro_arg_shift: mbe::Shift,
500 exp_map: Arc<mbe::TokenMap>,
504 pub fn expanded(&self) -> InFile<SyntaxNode> {
505 self.expanded.clone()
508 pub fn call_node(&self) -> Option<InFile<SyntaxNode>> {
509 Some(self.arg.with_value(self.arg.value.parent()?))
512 /// Map a token down from macro input into the macro expansion.
514 /// The inner workings of this function differ slightly depending on the type of macro we are dealing with:
516 /// For declarative macros, we need to accommodate for the macro definition site(which acts as a second unchanging input)
517 /// , as tokens can mapped in and out of it.
518 /// To do this we shift all ids in the expansion by the maximum id of the definition site giving us an easy
519 /// way to map all the tokens.
521 /// Attributes have two different inputs, the input tokentree in the attribute node and the item
522 /// the attribute is annotating. Similarly as for declarative macros we need to do a shift here
523 /// as well. Currently this is done by shifting the attribute input by the maximum id of the item.
524 /// - function-like and derives:
525 /// Both of these only have one simple call site input so no special handling is required here.
526 pub fn map_token_down(
528 db: &dyn db::AstDatabase,
529 item: Option<ast::Item>,
530 token: InFile<&SyntaxToken>,
531 ) -> Option<impl Iterator<Item = InFile<SyntaxToken>> + '_> {
532 assert_eq!(token.file_id, self.arg.file_id);
533 let token_id_in_attr_input = if let Some(item) = item {
534 // check if we are mapping down in an attribute input
535 // this is a special case as attributes can have two inputs
536 let call_id = self.expanded.file_id.macro_file()?.macro_call_id;
537 let loc = db.lookup_intern_macro_call(call_id);
539 let token_range = token.value.text_range();
541 MacroCallKind::Attr { attr_args, invoc_attr_index, is_derive, .. } => {
543 .doc_comments_and_attrs()
544 .nth(*invoc_attr_index as usize)
545 .and_then(Either::left)?;
546 match attr.token_tree() {
548 if token_tree.syntax().text_range().contains_range(token_range) =>
550 let attr_input_start =
551 token_tree.left_delimiter_token()?.text_range().start();
553 token.value.text_range().checked_sub(attr_input_start)?;
554 // shift by the item's tree's max id
555 let token_id = attr_args.1.token_by_range(relative_range)?;
556 let token_id = if *is_derive {
557 // we do not shift for `#[derive]`, as we only need to downmap the derive attribute tokens
560 self.macro_arg_shift.shift(token_id)
573 let token_id = match token_id_in_attr_input {
574 Some(token_id) => token_id,
575 // the token is not inside an attribute's input so do the lookup in the macro_arg as ususal
578 token.value.text_range().checked_sub(self.arg.value.text_range().start())?;
579 let token_id = self.macro_arg.1.token_by_range(relative_range)?;
580 // conditionally shift the id by a declaratives macro definition
581 self.macro_def.map_id_down(token_id)
587 .ranges_by_token(token_id, token.value.kind())
588 .flat_map(move |range| self.expanded.value.covering_element(range).into_token());
590 Some(tokens.map(move |token| self.expanded.with_value(token)))
593 /// Map a token up out of the expansion it resides in into the arguments of the macro call of the expansion.
596 db: &dyn db::AstDatabase,
597 token: InFile<&SyntaxToken>,
598 ) -> Option<(InFile<SyntaxToken>, Origin)> {
599 // Fetch the id through its text range,
600 let token_id = self.exp_map.token_by_range(token.value.text_range())?;
601 // conditionally unshifting the id to accommodate for macro-rules def site
602 let (mut token_id, origin) = self.macro_def.map_id_up(token_id);
604 let call_id = self.expanded.file_id.macro_file()?.macro_call_id;
605 let loc = db.lookup_intern_macro_call(call_id);
607 // Attributes are a bit special for us, they have two inputs, the input tokentree and the annotated item.
608 let (token_map, tt) = match &loc.kind {
609 MacroCallKind::Attr { attr_args, is_derive: true, .. } => {
610 (&attr_args.1, self.attr_input_or_mac_def.clone()?.syntax().cloned())
612 MacroCallKind::Attr { attr_args, .. } => {
613 // try unshifting the the token id, if unshifting fails, the token resides in the non-item attribute input
614 // note that the `TokenExpander::map_id_up` earlier only unshifts for declarative macros, so we don't double unshift with this
615 match self.macro_arg_shift.unshift(token_id) {
617 token_id = unshifted;
618 (&attr_args.1, self.attr_input_or_mac_def.clone()?.syntax().cloned())
620 None => (&self.macro_arg.1, self.arg.clone()),
624 mbe::Origin::Call => (&self.macro_arg.1, self.arg.clone()),
625 mbe::Origin::Def => match (&*self.macro_def, &self.attr_input_or_mac_def) {
626 (TokenExpander::DeclarativeMacro { def_site_token_map, .. }, Some(tt)) => {
627 (def_site_token_map, tt.syntax().cloned())
629 _ => panic!("`Origin::Def` used with non-`macro_rules!` macro"),
634 let range = token_map.first_range_by_token(token_id, token.value.kind())?;
636 tt.value.covering_element(range + tt.value.text_range().start()).into_token()?;
637 Some((tt.with_value(token), origin))
641 /// `AstId` points to an AST node in any file.
643 /// It is stable across reparses, and can be used as salsa key/value.
644 pub type AstId<N> = InFile<FileAstId<N>>;
646 impl<N: AstNode> AstId<N> {
647 pub fn to_node(&self, db: &dyn db::AstDatabase) -> N {
648 let root = db.parse_or_expand(self.file_id).unwrap();
649 db.ast_id_map(self.file_id).get(self.value).to_node(&root)
653 /// `InFile<T>` stores a value of `T` inside a particular file/syntax tree.
655 /// Typical usages are:
657 /// * `InFile<SyntaxNode>` -- syntax node in a file
658 /// * `InFile<ast::FnDef>` -- ast node in a file
659 /// * `InFile<TextSize>` -- offset in a file
660 #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
661 pub struct InFile<T> {
662 pub file_id: HirFileId,
667 pub fn new(file_id: HirFileId, value: T) -> InFile<T> {
668 InFile { file_id, value }
671 pub fn with_value<U>(&self, value: U) -> InFile<U> {
672 InFile::new(self.file_id, value)
675 pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> InFile<U> {
676 InFile::new(self.file_id, f(self.value))
678 pub fn as_ref(&self) -> InFile<&T> {
679 self.with_value(&self.value)
681 pub fn file_syntax(&self, db: &dyn db::AstDatabase) -> SyntaxNode {
682 db.parse_or_expand(self.file_id).expect("source created from invalid file")
686 impl<T: Clone> InFile<&T> {
687 pub fn cloned(&self) -> InFile<T> {
688 self.with_value(self.value.clone())
692 impl<T> InFile<Option<T>> {
693 pub fn transpose(self) -> Option<InFile<T>> {
694 let value = self.value?;
695 Some(InFile::new(self.file_id, value))
699 impl<'a> InFile<&'a SyntaxNode> {
700 pub fn ancestors_with_macros(
702 db: &dyn db::AstDatabase,
703 ) -> impl Iterator<Item = InFile<SyntaxNode>> + Clone + '_ {
704 iter::successors(Some(self.cloned()), move |node| match node.value.parent() {
705 Some(parent) => Some(node.with_value(parent)),
706 None => node.file_id.call_node(db),
710 /// Skips the attributed item that caused the macro invocation we are climbing up
711 pub fn ancestors_with_macros_skip_attr_item(
713 db: &dyn db::AstDatabase,
714 ) -> impl Iterator<Item = InFile<SyntaxNode>> + '_ {
715 let succ = move |node: &InFile<SyntaxNode>| match node.value.parent() {
716 Some(parent) => Some(node.with_value(parent)),
718 let parent_node = node.file_id.call_node(db)?;
719 if node.file_id.is_attr_macro(db) {
720 // macro call was an attributed item, skip it
721 // FIXME: does this fail if this is a direct expansion of another macro?
722 parent_node.map(|node| node.parent()).transpose()
728 iter::successors(succ(&self.cloned()), succ)
731 /// Falls back to the macro call range if the node cannot be mapped up fully.
732 pub fn original_file_range(self, db: &dyn db::AstDatabase) -> FileRange {
733 if let Some(res) = self.original_file_range_opt(db) {
737 // Fall back to whole macro call.
738 match self.file_id.0 {
739 HirFileIdRepr::FileId(file_id) => FileRange { file_id, range: self.value.text_range() },
740 HirFileIdRepr::MacroFile(mac_file) => {
741 let loc = db.lookup_intern_macro_call(mac_file.macro_call_id);
742 loc.kind.original_call_range(db)
747 /// Attempts to map the syntax node back up its macro calls.
748 pub fn original_file_range_opt(self, db: &dyn db::AstDatabase) -> Option<FileRange> {
749 match ascend_node_border_tokens(db, self) {
750 Some(InFile { file_id, value: (first, last) }) => {
751 let original_file = file_id.original_file(db);
752 let range = first.text_range().cover(last.text_range());
753 if file_id != original_file.into() {
754 tracing::error!("Failed mapping up more for {:?}", range);
757 Some(FileRange { file_id: original_file, range })
759 _ if !self.file_id.is_macro() => Some(FileRange {
760 file_id: self.file_id.original_file(db),
761 range: self.value.text_range(),
768 impl InFile<SyntaxToken> {
769 pub fn upmap(self, db: &dyn db::AstDatabase) -> Option<InFile<SyntaxToken>> {
770 let expansion = self.file_id.expansion_info(db)?;
771 expansion.map_token_up(db, self.as_ref()).map(|(it, _)| it)
775 fn ascend_node_border_tokens(
776 db: &dyn db::AstDatabase,
777 InFile { file_id, value: node }: InFile<&SyntaxNode>,
778 ) -> Option<InFile<(SyntaxToken, SyntaxToken)>> {
779 let expansion = file_id.expansion_info(db)?;
781 let first_token = |node: &SyntaxNode| skip_trivia_token(node.first_token()?, Direction::Next);
782 let last_token = |node: &SyntaxNode| skip_trivia_token(node.last_token()?, Direction::Prev);
784 let first = first_token(node)?;
785 let last = last_token(node)?;
786 let first = ascend_call_token(db, &expansion, InFile::new(file_id, first))?;
787 let last = ascend_call_token(db, &expansion, InFile::new(file_id, last))?;
788 (first.file_id == last.file_id).then(|| InFile::new(first.file_id, (first.value, last.value)))
791 fn ascend_call_token(
792 db: &dyn db::AstDatabase,
793 expansion: &ExpansionInfo,
794 token: InFile<SyntaxToken>,
795 ) -> Option<InFile<SyntaxToken>> {
796 let mut mapping = expansion.map_token_up(db, token.as_ref())?;
797 while let (mapped, Origin::Call) = mapping {
798 match mapped.file_id.expansion_info(db) {
799 Some(info) => mapping = info.map_token_up(db, mapped.as_ref())?,
800 None => return Some(mapped),
806 impl InFile<SyntaxToken> {
807 pub fn ancestors_with_macros(
809 db: &dyn db::AstDatabase,
810 ) -> impl Iterator<Item = InFile<SyntaxNode>> + '_ {
811 self.value.parent().into_iter().flat_map({
812 let file_id = self.file_id;
813 move |parent| InFile::new(file_id, &parent).ancestors_with_macros(db)
818 impl<N: AstNode> InFile<N> {
819 pub fn descendants<T: AstNode>(self) -> impl Iterator<Item = InFile<T>> {
820 self.value.syntax().descendants().filter_map(T::cast).map(move |n| self.with_value(n))
823 pub fn original_ast_node(self, db: &dyn db::AstDatabase) -> Option<InFile<N>> {
824 // This kind of upmapping can only be achieved in attribute expanded files,
825 // as we don't have node inputs otherwise and therefor can't find an `N` node in the input
826 if !self.file_id.is_macro() {
828 } else if !self.file_id.is_attr_macro(db) {
832 if let Some(InFile { file_id, value: (first, last) }) =
833 ascend_node_border_tokens(db, self.syntax())
835 if file_id.is_macro() {
836 let range = first.text_range().cover(last.text_range());
837 tracing::error!("Failed mapping out of macro file for {:?}", range);
840 // FIXME: This heuristic is brittle and with the right macro may select completely unrelated nodes
841 let anc = algo::least_common_ancestor(&first.parent()?, &last.parent()?)?;
842 let value = anc.ancestors().find_map(N::cast)?;
843 return Some(InFile::new(file_id, value));
848 pub fn syntax(&self) -> InFile<&SyntaxNode> {
849 self.with_value(self.value.syntax())
853 /// In Rust, macros expand token trees to token trees. When we want to turn a
854 /// token tree into an AST node, we need to figure out what kind of AST node we
855 /// want: something like `foo` can be a type, an expression, or a pattern.
857 /// Naively, one would think that "what this expands to" is a property of a
858 /// particular macro: macro `m1` returns an item, while macro `m2` returns an
859 /// expression, etc. That's not the case -- macros are polymorphic in the
860 /// result, and can expand to any type of the AST node.
862 /// What defines the actual AST node is the syntactic context of the macro
863 /// invocation. As a contrived example, in `let T![*] = T![*];` the first `T`
864 /// expands to a pattern, while the second one expands to an expression.
866 /// `ExpandTo` captures this bit of information about a particular macro call
868 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
878 pub fn from_call_site(call: &ast::MacroCall) -> ExpandTo {
879 use syntax::SyntaxKind::*;
881 let syn = call.syntax();
883 let parent = match syn.parent() {
885 None => return ExpandTo::Statements,
888 // FIXME: macros in statement position are treated as expression statements, they should
889 // probably be their own statement kind. The *grand*parent indicates what's valid.
890 if parent.kind() == MACRO_EXPR
893 .map_or(true, |p| matches!(p.kind(), EXPR_STMT | STMT_LIST | MACRO_STMTS))
895 return ExpandTo::Statements;
898 match parent.kind() {
899 MACRO_ITEMS | SOURCE_FILE | ITEM_LIST => ExpandTo::Items,
900 MACRO_STMTS | EXPR_STMT | STMT_LIST => ExpandTo::Statements,
901 MACRO_PAT => ExpandTo::Pattern,
902 MACRO_TYPE => ExpandTo::Type,
904 ARG_LIST | ARRAY_EXPR | AWAIT_EXPR | BIN_EXPR | BREAK_EXPR | CALL_EXPR | CAST_EXPR
905 | CLOSURE_EXPR | FIELD_EXPR | FOR_EXPR | IF_EXPR | INDEX_EXPR | LET_EXPR
906 | MATCH_ARM | MATCH_EXPR | MATCH_GUARD | METHOD_CALL_EXPR | PAREN_EXPR | PATH_EXPR
907 | PREFIX_EXPR | RANGE_EXPR | RECORD_EXPR_FIELD | REF_EXPR | RETURN_EXPR | TRY_EXPR
908 | TUPLE_EXPR | WHILE_EXPR | MACRO_EXPR => ExpandTo::Expr,
910 // Unknown , Just guess it is `Items`
918 pub struct UnresolvedMacro {