hir/
has_source.rs

1//! Provides set of implementation for hir's objects that allows get back location in file.
2
3use either::Either;
4use hir_def::{
5    CallableDefId, Lookup, MacroId, VariantId,
6    expr_store::ExpressionStore,
7    nameres::{ModuleOrigin, ModuleSource},
8    src::{HasChildSource, HasSource as _},
9};
10use hir_expand::{EditionedFileId, HirFileId, InFile};
11use hir_ty::{db::InternedClosure, next_solver::AnyImplId};
12use syntax::{AstNode, ast};
13use tt::TextRange;
14
15use crate::{
16    Adt, AnyFunctionId, Callee, Const, Enum, EnumVariant, ExternCrateDecl, Field, FieldSource,
17    Function, Impl, InlineAsmOperand, Label, LifetimeParam, LocalSource, Macro, Module, Param,
18    SelfParam, Static, Struct, Trait, TypeAlias, TypeOrConstParam, Union, Variant, db::HirDatabase,
19};
20
21pub trait HasSource: Sized {
22    type Ast: AstNode;
23    /// Fetches the definition's source node.
24    /// Using [`crate::SemanticsImpl::source`] is preferred when working with [`crate::Semantics`],
25    /// as that caches the parsed file in the semantics' cache.
26    ///
27    /// The current some implementations can return `InFile` instead of `Option<InFile>`.
28    /// But we made this method `Option` to support rlib in the future
29    /// by <https://github.com/rust-lang/rust-analyzer/issues/6913>
30    fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>>;
31
32    /// Fetches the source node, along with its full range.
33    ///
34    /// The reason for the separate existence of this method is that some things, notably builtin derive impls,
35    /// do not really have a source node, at least not of the correct type. But we still can trace them
36    /// to source code (the derive producing them). So this method will return the range if it is supported,
37    /// and if the node is supported too it will return it as well.
38    fn source_with_range(
39        self,
40        db: &dyn HirDatabase,
41    ) -> Option<InFile<(TextRange, Option<Self::Ast>)>> {
42        let source = self.source(db)?;
43        Some(source.map(|node| (node.syntax().text_range(), Some(node))))
44    }
45}
46
47/// NB: Module is !HasSource, because it has two source nodes at the same time:
48/// definition and declaration.
49impl Module {
50    /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
51    pub fn definition_source(self, db: &dyn HirDatabase) -> InFile<ModuleSource> {
52        let def_map = self.id.def_map(db);
53        def_map[self.id].definition_source(db)
54    }
55
56    /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
57    pub fn definition_source_range(self, db: &dyn HirDatabase) -> InFile<TextRange> {
58        let def_map = self.id.def_map(db);
59        def_map[self.id].definition_source_range(db)
60    }
61
62    pub fn definition_source_file_id(self, db: &dyn HirDatabase) -> HirFileId {
63        let def_map = self.id.def_map(db);
64        def_map[self.id].definition_source_file_id()
65    }
66
67    pub fn is_mod_rs(self, db: &dyn HirDatabase) -> bool {
68        let def_map = self.id.def_map(db);
69        match def_map[self.id].origin {
70            ModuleOrigin::File { is_mod_rs, .. } => is_mod_rs,
71            _ => false,
72        }
73    }
74
75    pub fn as_source_file_id(self, db: &dyn HirDatabase) -> Option<EditionedFileId> {
76        let def_map = self.id.def_map(db);
77        match def_map[self.id].origin {
78            ModuleOrigin::File { definition, .. } | ModuleOrigin::CrateRoot { definition, .. } => {
79                Some(definition)
80            }
81            _ => None,
82        }
83    }
84
85    pub fn is_inline(self, db: &dyn HirDatabase) -> bool {
86        let def_map = self.id.def_map(db);
87        def_map[self.id].origin.is_inline()
88    }
89
90    /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
91    /// `None` for the crate root.
92    pub fn declaration_source(self, db: &dyn HirDatabase) -> Option<InFile<ast::Module>> {
93        let def_map = self.id.def_map(db);
94        def_map[self.id].declaration_source(db)
95    }
96
97    /// Returns a text range which declares this module, either a `mod foo;` or a `mod foo {}`.
98    /// `None` for the crate root.
99    pub fn declaration_source_range(self, db: &dyn HirDatabase) -> Option<InFile<TextRange>> {
100        let def_map = self.id.def_map(db);
101        def_map[self.id].declaration_source_range(db)
102    }
103}
104
105impl HasSource for Field {
106    type Ast = FieldSource;
107    fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
108        let var = VariantId::from(self.parent);
109        let src = var.child_source(db);
110        let field_source = src.map(|it| match it[self.id].clone() {
111            Either::Left(it) => FieldSource::Pos(it),
112            Either::Right(it) => FieldSource::Named(it),
113        });
114        Some(field_source)
115    }
116}
117impl HasSource for Adt {
118    type Ast = ast::Adt;
119    fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
120        match self {
121            Adt::Struct(s) => Some(s.source(db)?.map(ast::Adt::Struct)),
122            Adt::Union(u) => Some(u.source(db)?.map(ast::Adt::Union)),
123            Adt::Enum(e) => Some(e.source(db)?.map(ast::Adt::Enum)),
124        }
125    }
126}
127impl HasSource for Variant {
128    type Ast = ast::VariantDef;
129    fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
130        match self {
131            Variant::Struct(s) => Some(s.source(db)?.map(ast::VariantDef::Struct)),
132            Variant::Union(u) => Some(u.source(db)?.map(ast::VariantDef::Union)),
133            Variant::EnumVariant(v) => Some(v.source(db)?.map(ast::VariantDef::Variant)),
134        }
135    }
136}
137impl HasSource for Struct {
138    type Ast = ast::Struct;
139    fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
140        Some(self.id.lookup(db).source(db))
141    }
142}
143impl HasSource for Union {
144    type Ast = ast::Union;
145    fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
146        Some(self.id.lookup(db).source(db))
147    }
148}
149impl HasSource for Enum {
150    type Ast = ast::Enum;
151    fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
152        Some(self.id.lookup(db).source(db))
153    }
154}
155impl HasSource for EnumVariant {
156    type Ast = ast::Variant;
157    fn source(self, db: &dyn HirDatabase) -> Option<InFile<ast::Variant>> {
158        Some(self.id.lookup(db).source(db))
159    }
160}
161impl HasSource for Function {
162    type Ast = ast::Fn;
163    fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
164        match self.id {
165            AnyFunctionId::FunctionId(id) => Some(id.loc(db).source(db)),
166            // When calling `source()`, we use the trait method source, but when calling `source_with_range()`,
167            // we return `None` as the syntax node source. This is relying on the assumption that if you are calling
168            // `source_with_range()` (e.g. in navigation) you're prepared to deal with no source node, while if
169            // you call `source()` maybe you don't - therefore we fall back to the trait method, to not lose features.
170            AnyFunctionId::BuiltinDeriveImplMethod { method, impl_ } => method
171                .trait_method(db, impl_)
172                .and_then(|trait_method| Function::from(trait_method).source(db)),
173        }
174    }
175
176    fn source_with_range(
177        self,
178        db: &dyn HirDatabase,
179    ) -> Option<InFile<(TextRange, Option<Self::Ast>)>> {
180        match self.id {
181            AnyFunctionId::FunctionId(id) => Some(
182                id.loc(db).source(db).map(|source| (source.syntax().text_range(), Some(source))),
183            ),
184            AnyFunctionId::BuiltinDeriveImplMethod { impl_, .. } => {
185                Some(impl_.loc(db).source(db).map(|range| (range, None)))
186            }
187        }
188    }
189}
190impl HasSource for Const {
191    type Ast = ast::Const;
192    fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
193        Some(self.id.lookup(db).source(db))
194    }
195}
196impl HasSource for Static {
197    type Ast = ast::Static;
198    fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
199        Some(self.id.lookup(db).source(db))
200    }
201}
202impl HasSource for Trait {
203    type Ast = ast::Trait;
204    fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
205        Some(self.id.lookup(db).source(db))
206    }
207}
208impl HasSource for TypeAlias {
209    type Ast = ast::TypeAlias;
210    fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
211        Some(self.id.lookup(db).source(db))
212    }
213}
214impl HasSource for Macro {
215    type Ast = Either<ast::Macro, ast::Fn>;
216    fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
217        match self.id {
218            MacroId::Macro2Id(it) => {
219                Some(it.lookup(db).source(db).map(ast::Macro::MacroDef).map(Either::Left))
220            }
221            MacroId::MacroRulesId(it) => {
222                Some(it.lookup(db).source(db).map(ast::Macro::MacroRules).map(Either::Left))
223            }
224            MacroId::ProcMacroId(it) => Some(it.lookup(db).source(db).map(Either::Right)),
225        }
226    }
227}
228impl HasSource for Impl {
229    type Ast = ast::Impl;
230    fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
231        match self.id {
232            AnyImplId::ImplId(id) => Some(id.loc(db).source(db)),
233            AnyImplId::BuiltinDeriveImplId(_) => None,
234        }
235    }
236
237    fn source_with_range(
238        self,
239        db: &dyn HirDatabase,
240    ) -> Option<InFile<(TextRange, Option<Self::Ast>)>> {
241        match self.id {
242            AnyImplId::ImplId(id) => Some(
243                id.loc(db).source(db).map(|source| (source.syntax().text_range(), Some(source))),
244            ),
245            AnyImplId::BuiltinDeriveImplId(impl_) => {
246                Some(impl_.loc(db).source(db).map(|range| (range, None)))
247            }
248        }
249    }
250}
251
252impl HasSource for TypeOrConstParam {
253    type Ast = Either<ast::TypeOrConstParam, ast::Trait>;
254    fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
255        let child_source = self.id.parent.child_source(db);
256        child_source.map(|it| it.get(self.id.local_id).cloned()).transpose()
257    }
258}
259
260impl HasSource for LifetimeParam {
261    type Ast = ast::LifetimeParam;
262    fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
263        let child_source = self.id.parent.child_source(db);
264        child_source.map(|it| it.get(self.id.local_id).cloned()).transpose()
265    }
266}
267
268impl HasSource for LocalSource {
269    type Ast = Either<ast::IdentPat, ast::SelfParam>;
270
271    fn source(self, _: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
272        Some(self.source)
273    }
274}
275
276impl HasSource for Param<'_> {
277    type Ast = Either<ast::SelfParam, ast::Param>;
278
279    fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
280        match self.func {
281            Callee::Def(CallableDefId::FunctionId(func)) => {
282                let InFile { file_id, value } = Function::from(func).source(db)?;
283                let params = value.param_list()?;
284                if let Some(self_param) = params.self_param() {
285                    if let Some(idx) = self.idx.checked_sub(1) {
286                        params.params().nth(idx).map(Either::Right)
287                    } else {
288                        Some(Either::Left(self_param))
289                    }
290                } else {
291                    params.params().nth(self.idx).map(Either::Right)
292                }
293                .map(|value| InFile { file_id, value })
294            }
295            Callee::Closure(closure, _) => {
296                let InternedClosure(owner, expr_id) = db.lookup_intern_closure(closure);
297                let (_, source_map) = ExpressionStore::with_source_map(db, owner);
298                let ast @ InFile { file_id, value } = source_map.expr_syntax(expr_id).ok()?;
299                let root = db.parse_or_expand(file_id);
300                match value.to_node(&root) {
301                    Either::Left(ast::Expr::ClosureExpr(it)) => it
302                        .param_list()?
303                        .params()
304                        .nth(self.idx)
305                        .map(Either::Right)
306                        .map(|value| InFile { file_id: ast.file_id, value }),
307                    _ => None,
308                }
309            }
310            _ => None,
311        }
312    }
313}
314
315impl HasSource for SelfParam {
316    type Ast = ast::SelfParam;
317
318    fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
319        let InFile { file_id, value } = self.func.source(db)?;
320        value
321            .param_list()
322            .and_then(|params| params.self_param())
323            .map(|value| InFile { file_id, value })
324    }
325}
326
327impl HasSource for Label {
328    type Ast = ast::Label;
329
330    fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
331        let src = ExpressionStore::with_source_map(db, self.parent).1.label_syntax(self.label_id);
332        let root = src.file_syntax(db);
333        src.map(|ast| ast.to_node(&root).left()).transpose()
334    }
335}
336
337impl HasSource for ExternCrateDecl {
338    type Ast = ast::ExternCrate;
339
340    fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
341        Some(self.id.lookup(db).source(db))
342    }
343}
344
345impl HasSource for InlineAsmOperand {
346    type Ast = ast::AsmOperandNamed;
347    fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
348        let (_, source_map) = ExpressionStore::with_source_map(db, self.owner);
349        if let Ok(src) = source_map.expr_syntax(self.expr) {
350            let root = src.file_syntax(db);
351            return src
352                .map(|ast| match ast.to_node(&root) {
353                    Either::Left(ast::Expr::AsmExpr(asm)) => asm
354                        .asm_pieces()
355                        .filter_map(|it| match it {
356                            ast::AsmPiece::AsmOperandNamed(it) => Some(it),
357                            _ => None,
358                        })
359                        .nth(self.index),
360                    _ => None,
361                })
362                .transpose();
363        }
364        None
365    }
366}