3 use relative_path::RelativePathBuf;
4 use ra_db::{CrateId, SourceRootId, Edition};
5 use ra_syntax::{ast::self, TreeArc, SyntaxNode};
8 Name, ScopesWithSyntaxMapping, Ty, HirFileId,
9 HirDatabase, PersistentHirDatabase,
11 nameres::{ModuleScope, Namespace, lower::ImportId},
12 expr::{Body, BodySyntaxMapping},
14 adt::{EnumVariantId, StructFieldId, VariantDef},
15 generics::GenericParams,
16 docs::{Documentation, Docs, docs_from_ast},
17 module_tree::ModuleId,
18 ids::{FunctionId, StructId, EnumId, AstItemDef, ConstId, StaticId, TraitId, TypeId},
19 impl_block::ImplBlock,
23 /// hir::Crate describes a single crate. It's the main interface with which
24 /// a crate's dependencies interact. Mostly, it should be just a proxy for the
26 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
28 pub(crate) crate_id: CrateId,
32 pub struct CrateDependency {
38 pub fn crate_id(&self) -> CrateId {
42 pub fn dependencies(&self, db: &impl PersistentHirDatabase) -> Vec<CrateDependency> {
43 self.dependencies_impl(db)
46 pub fn root_module(&self, db: &impl PersistentHirDatabase) -> Option<Module> {
47 self.root_module_impl(db)
50 pub fn edition(&self, db: &impl PersistentHirDatabase) -> Edition {
51 let crate_graph = db.crate_graph();
52 crate_graph.edition(self.crate_id)
55 // TODO: should this be in source_binder?
56 pub fn source_root_crates(
57 db: &impl PersistentHirDatabase,
58 source_root: SourceRootId,
60 let crate_ids = db.source_root_crates(source_root);
61 crate_ids.iter().map(|&crate_id| Crate { crate_id }).collect()
70 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
72 pub(crate) krate: Crate,
73 pub(crate) module_id: ModuleId,
76 /// The defs which can be visible in the module.
77 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
83 // Can't be directly declared, but can be imported.
84 EnumVariant(EnumVariant),
90 impl_froms!(ModuleDef: Module, Function, Struct, Enum, EnumVariant, Const, Static, Trait, Type);
92 pub enum ModuleSource {
93 SourceFile(TreeArc<ast::SourceFile>),
94 Module(TreeArc<ast::Module>),
97 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
99 UnresolvedModule { candidate: RelativePathBuf },
100 NotDirOwner { move_to: RelativePathBuf, candidate: RelativePathBuf },
104 /// Name of this module.
105 pub fn name(&self, db: &impl HirDatabase) -> Option<Name> {
109 /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
110 pub fn definition_source(&self, db: &impl PersistentHirDatabase) -> (HirFileId, ModuleSource) {
111 self.definition_source_impl(db)
114 /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
115 /// `None` for the crate root.
116 pub fn declaration_source(
118 db: &impl HirDatabase,
119 ) -> Option<(HirFileId, TreeArc<ast::Module>)> {
120 self.declaration_source_impl(db)
123 /// Returns the syntax of the last path segment corresponding to this import
124 pub fn import_source(
126 db: &impl HirDatabase,
128 ) -> TreeArc<ast::PathSegment> {
129 self.import_source_impl(db, import)
132 /// Returns the crate this module is part of.
133 pub fn krate(&self, _db: &impl PersistentHirDatabase) -> Option<Crate> {
137 /// Topmost parent of this module. Every module has a `crate_root`, but some
138 /// might be missing `krate`. This can happen if a module's file is not included
139 /// in the module tree of any target in `Cargo.toml`.
140 pub fn crate_root(&self, db: &impl PersistentHirDatabase) -> Module {
141 self.crate_root_impl(db)
144 /// Finds a child module with the specified name.
145 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Option<Module> {
146 self.child_impl(db, name)
149 /// Iterates over all child modules.
150 pub fn children(&self, db: &impl PersistentHirDatabase) -> impl Iterator<Item = Module> {
151 self.children_impl(db)
154 /// Finds a parent module.
155 pub fn parent(&self, db: &impl PersistentHirDatabase) -> Option<Module> {
159 pub fn path_to_root(&self, db: &impl HirDatabase) -> Vec<Module> {
160 let mut res = vec![self.clone()];
161 let mut curr = self.clone();
162 while let Some(next) = curr.parent(db) {
163 res.push(next.clone());
169 /// Returns a `ModuleScope`: a set of items, visible in this module.
170 pub fn scope(&self, db: &impl HirDatabase) -> ModuleScope {
171 db.item_map(self.krate)[self.module_id].clone()
174 pub fn problems(&self, db: &impl HirDatabase) -> Vec<(TreeArc<SyntaxNode>, Problem)> {
175 self.problems_impl(db)
178 pub fn resolver(&self, db: &impl HirDatabase) -> Resolver {
179 let item_map = db.item_map(self.krate);
180 Resolver::default().push_module_scope(item_map, *self)
183 pub fn declarations(self, db: &impl HirDatabase) -> Vec<ModuleDef> {
184 let (lowered_module, _) = db.lower_module(self);
190 per_ns.take_types().into_iter().chain(per_ns.take_values().into_iter())
195 pub fn impl_blocks(self, db: &impl HirDatabase) -> Vec<ImplBlock> {
196 let module_impl_blocks = db.impls_in_module(self);
200 .map(|(impl_id, _)| ImplBlock::from_id(self, impl_id))
205 impl Docs for Module {
206 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
207 self.declaration_source(db).and_then(|it| docs_from_ast(&*it.1))
211 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
212 pub struct StructField {
213 pub(crate) parent: VariantDef,
214 pub(crate) id: StructFieldId,
218 pub enum FieldSource {
219 Named(TreeArc<ast::NamedFieldDef>),
220 Pos(TreeArc<ast::PosFieldDef>),
224 pub fn name(&self, db: &impl HirDatabase) -> Name {
225 self.parent.variant_data(db).fields().unwrap()[self.id].name.clone()
228 pub fn source(&self, db: &impl PersistentHirDatabase) -> (HirFileId, FieldSource) {
232 pub fn ty(&self, db: &impl HirDatabase) -> Ty {
233 db.type_for_field(*self)
236 pub fn parent_def(&self, _db: &impl HirDatabase) -> VariantDef {
241 impl Docs for StructField {
242 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
243 match self.source(db).1 {
244 FieldSource::Named(named) => docs_from_ast(&*named),
245 FieldSource::Pos(..) => return None,
250 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
252 pub(crate) id: StructId,
256 pub fn source(&self, db: &impl PersistentHirDatabase) -> (HirFileId, TreeArc<ast::StructDef>) {
260 pub fn module(&self, db: &impl HirDatabase) -> Module {
264 pub fn name(&self, db: &impl HirDatabase) -> Option<Name> {
265 db.struct_data(*self).name.clone()
268 pub fn fields(&self, db: &impl HirDatabase) -> Vec<StructField> {
269 db.struct_data(*self)
273 .flat_map(|it| it.iter())
274 .map(|(id, _)| StructField { parent: (*self).into(), id })
278 pub fn field(&self, db: &impl HirDatabase, name: &Name) -> Option<StructField> {
279 db.struct_data(*self)
283 .flat_map(|it| it.iter())
284 .find(|(_id, data)| data.name == *name)
285 .map(|(id, _)| StructField { parent: (*self).into(), id })
288 pub fn generic_params(&self, db: &impl PersistentHirDatabase) -> Arc<GenericParams> {
289 db.generic_params((*self).into())
292 pub fn ty(&self, db: &impl HirDatabase) -> Ty {
293 db.type_for_def((*self).into(), Namespace::Types)
296 pub fn constructor_ty(&self, db: &impl HirDatabase) -> Ty {
297 db.type_for_def((*self).into(), Namespace::Values)
300 // TODO move to a more general type
301 /// Builds a resolver for type references inside this struct.
302 pub fn resolver(&self, db: &impl HirDatabase) -> Resolver {
303 // take the outer scope...
304 let r = self.module(db).resolver(db);
305 // ...and add generic params, if present
306 let p = self.generic_params(db);
307 let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r };
312 impl Docs for Struct {
313 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
314 docs_from_ast(&*self.source(db).1)
318 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
320 pub(crate) id: EnumId,
324 pub fn source(&self, db: &impl PersistentHirDatabase) -> (HirFileId, TreeArc<ast::EnumDef>) {
328 pub fn module(&self, db: &impl HirDatabase) -> Module {
332 pub fn name(&self, db: &impl HirDatabase) -> Option<Name> {
333 db.enum_data(*self).name.clone()
336 pub fn variants(&self, db: &impl PersistentHirDatabase) -> Vec<EnumVariant> {
340 .map(|(id, _)| EnumVariant { parent: *self, id })
344 pub fn variant(&self, db: &impl PersistentHirDatabase, name: &Name) -> Option<EnumVariant> {
348 .find(|(_id, data)| data.name.as_ref() == Some(name))
349 .map(|(id, _)| EnumVariant { parent: *self, id })
352 pub fn generic_params(&self, db: &impl PersistentHirDatabase) -> Arc<GenericParams> {
353 db.generic_params((*self).into())
356 pub fn ty(&self, db: &impl HirDatabase) -> Ty {
357 db.type_for_def((*self).into(), Namespace::Types)
360 // TODO: move to a more general type
361 /// Builds a resolver for type references inside this struct.
362 pub fn resolver(&self, db: &impl HirDatabase) -> Resolver {
363 // take the outer scope...
364 let r = self.module(db).resolver(db);
365 // ...and add generic params, if present
366 let p = self.generic_params(db);
367 let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r };
373 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
374 docs_from_ast(&*self.source(db).1)
378 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
379 pub struct EnumVariant {
380 pub(crate) parent: Enum,
381 pub(crate) id: EnumVariantId,
387 db: &impl PersistentHirDatabase,
388 ) -> (HirFileId, TreeArc<ast::EnumVariant>) {
391 pub fn module(&self, db: &impl HirDatabase) -> Module {
392 self.parent.module(db)
394 pub fn parent_enum(&self, _db: &impl PersistentHirDatabase) -> Enum {
398 pub fn name(&self, db: &impl PersistentHirDatabase) -> Option<Name> {
399 db.enum_data(self.parent).variants[self.id].name.clone()
402 pub fn fields(&self, db: &impl HirDatabase) -> Vec<StructField> {
403 self.variant_data(db)
406 .flat_map(|it| it.iter())
407 .map(|(id, _)| StructField { parent: (*self).into(), id })
411 pub fn field(&self, db: &impl HirDatabase, name: &Name) -> Option<StructField> {
412 self.variant_data(db)
415 .flat_map(|it| it.iter())
416 .find(|(_id, data)| data.name == *name)
417 .map(|(id, _)| StructField { parent: (*self).into(), id })
421 impl Docs for EnumVariant {
422 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
423 docs_from_ast(&*self.source(db).1)
427 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
428 pub struct Function {
429 pub(crate) id: FunctionId,
432 /// The declared signature of a function.
433 #[derive(Debug, Clone, PartialEq, Eq)]
434 pub struct FnSignature {
435 pub(crate) name: Name,
436 pub(crate) params: Vec<TypeRef>,
437 pub(crate) ret_type: TypeRef,
438 /// True if the first param is `self`. This is relevant to decide whether this
439 /// can be called as a method.
440 pub(crate) has_self_param: bool,
444 pub fn name(&self) -> &Name {
448 pub fn params(&self) -> &[TypeRef] {
452 pub fn ret_type(&self) -> &TypeRef {
456 /// True if the first arg is `self`. This is relevant to decide whether this
457 /// can be called as a method.
458 pub fn has_self_param(&self) -> bool {
464 pub fn source(&self, db: &impl PersistentHirDatabase) -> (HirFileId, TreeArc<ast::FnDef>) {
468 pub fn module(&self, db: &impl PersistentHirDatabase) -> Module {
472 pub fn name(&self, db: &impl HirDatabase) -> Name {
473 self.signature(db).name.clone()
476 pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Arc<BodySyntaxMapping> {
477 db.body_syntax_mapping(*self)
480 pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> {
484 pub fn ty(&self, db: &impl HirDatabase) -> Ty {
485 db.type_for_def((*self).into(), Namespace::Values)
488 pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSyntaxMapping {
489 let scopes = db.expr_scopes(*self);
490 let syntax_mapping = db.body_syntax_mapping(*self);
491 ScopesWithSyntaxMapping { scopes, syntax_mapping }
494 pub fn signature(&self, db: &impl HirDatabase) -> Arc<FnSignature> {
495 db.fn_signature(*self)
498 pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> {
502 pub fn generic_params(&self, db: &impl PersistentHirDatabase) -> Arc<GenericParams> {
503 db.generic_params((*self).into())
506 /// The containing impl block, if this is a method.
507 pub fn impl_block(&self, db: &impl PersistentHirDatabase) -> Option<ImplBlock> {
508 let module_impls = db.impls_in_module(self.module(db));
509 ImplBlock::containing(module_impls, (*self).into())
512 // TODO: move to a more general type for 'body-having' items
513 /// Builds a resolver for code inside this item.
514 pub fn resolver(&self, db: &impl HirDatabase) -> Resolver {
515 // take the outer scope...
518 .map(|ib| ib.resolver(db))
519 .unwrap_or_else(|| self.module(db).resolver(db));
520 // ...and add generic params, if present
521 let p = self.generic_params(db);
522 let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r };
527 impl Docs for Function {
528 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
529 docs_from_ast(&*self.source(db).1)
533 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
535 pub(crate) id: ConstId,
539 pub fn source(&self, db: &impl PersistentHirDatabase) -> (HirFileId, TreeArc<ast::ConstDef>) {
543 pub fn module(&self, db: &impl PersistentHirDatabase) -> Module {
547 /// The containing impl block, if this is a method.
548 pub fn impl_block(&self, db: &impl PersistentHirDatabase) -> Option<ImplBlock> {
549 let module_impls = db.impls_in_module(self.module(db));
550 ImplBlock::containing(module_impls, (*self).into())
554 impl Docs for Const {
555 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
556 docs_from_ast(&*self.source(db).1)
560 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
562 pub(crate) id: StaticId,
566 pub fn source(&self, db: &impl PersistentHirDatabase) -> (HirFileId, TreeArc<ast::StaticDef>) {
570 pub fn module(&self, db: &impl PersistentHirDatabase) -> Module {
575 impl Docs for Static {
576 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
577 docs_from_ast(&*self.source(db).1)
581 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
583 pub(crate) id: TraitId,
587 pub fn source(&self, db: &impl PersistentHirDatabase) -> (HirFileId, TreeArc<ast::TraitDef>) {
591 pub fn module(&self, db: &impl PersistentHirDatabase) -> Module {
595 pub fn generic_params(&self, db: &impl PersistentHirDatabase) -> Arc<GenericParams> {
596 db.generic_params((*self).into())
600 impl Docs for Trait {
601 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
602 docs_from_ast(&*self.source(db).1)
606 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
608 pub(crate) id: TypeId,
612 pub fn source(&self, db: &impl PersistentHirDatabase) -> (HirFileId, TreeArc<ast::TypeDef>) {
616 pub fn generic_params(&self, db: &impl PersistentHirDatabase) -> Arc<GenericParams> {
617 db.generic_params((*self).into())
620 pub fn module(&self, db: &impl PersistentHirDatabase) -> Module {
624 /// The containing impl block, if this is a method.
625 pub fn impl_block(&self, db: &impl PersistentHirDatabase) -> Option<ImplBlock> {
626 let module_impls = db.impls_in_module(self.module(db));
627 ImplBlock::containing(module_impls, (*self).into())
630 pub fn type_ref(self, db: &impl PersistentHirDatabase) -> Arc<TypeRef> {
631 db.type_alias_ref(self)
634 /// Builds a resolver for the type references in this type alias.
635 pub fn resolver(&self, db: &impl HirDatabase) -> Resolver {
636 // take the outer scope...
639 .map(|ib| ib.resolver(db))
640 .unwrap_or_else(|| self.module(db).resolver(db));
641 // ...and add generic params, if present
642 let p = self.generic_params(db);
643 let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r };
649 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
650 docs_from_ast(&*self.source(db).1)