diff options
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 80 | ||||
-rw-r--r-- | crates/ra_hir/src/from_source.rs | 80 | ||||
-rw-r--r-- | crates/ra_hir/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 8 |
4 files changed, 68 insertions, 102 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 7850ea9a7..ecf883272 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -11,9 +11,9 @@ use hir_def::{ | |||
11 | per_ns::PerNs, | 11 | per_ns::PerNs, |
12 | resolver::HasResolver, | 12 | resolver::HasResolver, |
13 | type_ref::{Mutability, TypeRef}, | 13 | type_ref::{Mutability, TypeRef}, |
14 | AdtId, ConstId, ContainerId, DefWithBodyId, EnumId, FunctionId, HasModule, ImplId, | 14 | AdtId, ConstId, DefWithBodyId, EnumId, FunctionId, HasModule, ImplId, LocalEnumVariantId, |
15 | LocalEnumVariantId, LocalImportId, LocalModuleId, LocalStructFieldId, Lookup, ModuleId, | 15 | LocalImportId, LocalModuleId, LocalStructFieldId, Lookup, ModuleId, StaticId, StructId, |
16 | StaticId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId, | 16 | TraitId, TypeAliasId, TypeParamId, UnionId, |
17 | }; | 17 | }; |
18 | use hir_expand::{ | 18 | use hir_expand::{ |
19 | diagnostics::DiagnosticSink, | 19 | diagnostics::DiagnosticSink, |
@@ -269,7 +269,7 @@ pub struct Struct { | |||
269 | 269 | ||
270 | impl Struct { | 270 | impl Struct { |
271 | pub fn module(self, db: &impl DefDatabase) -> Module { | 271 | pub fn module(self, db: &impl DefDatabase) -> Module { |
272 | Module { id: self.id.lookup(db).container } | 272 | Module { id: self.id.lookup(db).container.module(db) } |
273 | } | 273 | } |
274 | 274 | ||
275 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { | 275 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { |
@@ -290,7 +290,7 @@ impl Struct { | |||
290 | } | 290 | } |
291 | 291 | ||
292 | pub fn ty(self, db: &impl HirDatabase) -> Type { | 292 | pub fn ty(self, db: &impl HirDatabase) -> Type { |
293 | Type::from_def(db, self.id.lookup(db).container.krate, self.id) | 293 | Type::from_def(db, self.id.lookup(db).container.module(db).krate, self.id) |
294 | } | 294 | } |
295 | 295 | ||
296 | fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> { | 296 | fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> { |
@@ -309,11 +309,11 @@ impl Union { | |||
309 | } | 309 | } |
310 | 310 | ||
311 | pub fn module(self, db: &impl DefDatabase) -> Module { | 311 | pub fn module(self, db: &impl DefDatabase) -> Module { |
312 | Module { id: self.id.lookup(db).container } | 312 | Module { id: self.id.lookup(db).container.module(db) } |
313 | } | 313 | } |
314 | 314 | ||
315 | pub fn ty(self, db: &impl HirDatabase) -> Type { | 315 | pub fn ty(self, db: &impl HirDatabase) -> Type { |
316 | Type::from_def(db, self.id.lookup(db).container.krate, self.id) | 316 | Type::from_def(db, self.id.lookup(db).container.module(db).krate, self.id) |
317 | } | 317 | } |
318 | 318 | ||
319 | pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> { | 319 | pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> { |
@@ -337,7 +337,7 @@ pub struct Enum { | |||
337 | 337 | ||
338 | impl Enum { | 338 | impl Enum { |
339 | pub fn module(self, db: &impl DefDatabase) -> Module { | 339 | pub fn module(self, db: &impl DefDatabase) -> Module { |
340 | Module { id: self.id.lookup(db).container } | 340 | Module { id: self.id.lookup(db).container.module(db) } |
341 | } | 341 | } |
342 | 342 | ||
343 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { | 343 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { |
@@ -357,7 +357,7 @@ impl Enum { | |||
357 | } | 357 | } |
358 | 358 | ||
359 | pub fn ty(self, db: &impl HirDatabase) -> Type { | 359 | pub fn ty(self, db: &impl HirDatabase) -> Type { |
360 | Type::from_def(db, self.id.lookup(db).container.krate, self.id) | 360 | Type::from_def(db, self.id.lookup(db).container.module(db).krate, self.id) |
361 | } | 361 | } |
362 | } | 362 | } |
363 | 363 | ||
@@ -529,30 +529,6 @@ impl Const { | |||
529 | pub fn name(self, db: &impl HirDatabase) -> Option<Name> { | 529 | pub fn name(self, db: &impl HirDatabase) -> Option<Name> { |
530 | db.const_data(self.id).name.clone() | 530 | db.const_data(self.id).name.clone() |
531 | } | 531 | } |
532 | |||
533 | /// The containing impl block, if this is a type alias. | ||
534 | pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> { | ||
535 | match self.container(db) { | ||
536 | Some(Container::ImplBlock(it)) => Some(it), | ||
537 | _ => None, | ||
538 | } | ||
539 | } | ||
540 | |||
541 | /// The containing trait, if this is a trait type alias definition. | ||
542 | pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> { | ||
543 | match self.container(db) { | ||
544 | Some(Container::Trait(it)) => Some(it), | ||
545 | _ => None, | ||
546 | } | ||
547 | } | ||
548 | |||
549 | pub fn container(self, db: &impl DefDatabase) -> Option<Container> { | ||
550 | match self.id.lookup(db).container { | ||
551 | ContainerId::TraitId(it) => Some(Container::Trait(it.into())), | ||
552 | ContainerId::ImplId(it) => Some(Container::ImplBlock(it.into())), | ||
553 | ContainerId::ModuleId(_) => None, | ||
554 | } | ||
555 | } | ||
556 | } | 532 | } |
557 | 533 | ||
558 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 534 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
@@ -577,7 +553,7 @@ pub struct Trait { | |||
577 | 553 | ||
578 | impl Trait { | 554 | impl Trait { |
579 | pub fn module(self, db: &impl DefDatabase) -> Module { | 555 | pub fn module(self, db: &impl DefDatabase) -> Module { |
580 | Module { id: self.id.lookup(db).container } | 556 | Module { id: self.id.lookup(db).container.module(db) } |
581 | } | 557 | } |
582 | 558 | ||
583 | pub fn name(self, db: &impl DefDatabase) -> Name { | 559 | pub fn name(self, db: &impl DefDatabase) -> Name { |
@@ -612,30 +588,6 @@ impl TypeAlias { | |||
612 | Some(self.module(db).krate()) | 588 | Some(self.module(db).krate()) |
613 | } | 589 | } |
614 | 590 | ||
615 | /// The containing impl block, if this is a type alias. | ||
616 | pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> { | ||
617 | match self.container(db) { | ||
618 | Some(Container::ImplBlock(it)) => Some(it), | ||
619 | _ => None, | ||
620 | } | ||
621 | } | ||
622 | |||
623 | /// The containing trait, if this is a trait type alias definition. | ||
624 | pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> { | ||
625 | match self.container(db) { | ||
626 | Some(Container::Trait(it)) => Some(it), | ||
627 | _ => None, | ||
628 | } | ||
629 | } | ||
630 | |||
631 | pub fn container(self, db: &impl DefDatabase) -> Option<Container> { | ||
632 | match self.id.lookup(db).container { | ||
633 | ContainerId::TraitId(it) => Some(Container::Trait(it.into())), | ||
634 | ContainerId::ImplId(it) => Some(Container::ImplBlock(it.into())), | ||
635 | ContainerId::ModuleId(_) => None, | ||
636 | } | ||
637 | } | ||
638 | |||
639 | pub fn type_ref(self, db: &impl DefDatabase) -> Option<TypeRef> { | 591 | pub fn type_ref(self, db: &impl DefDatabase) -> Option<TypeRef> { |
640 | db.type_alias_data(self.id).type_ref.clone() | 592 | db.type_alias_data(self.id).type_ref.clone() |
641 | } | 593 | } |
@@ -654,14 +606,6 @@ pub struct MacroDef { | |||
654 | pub(crate) id: MacroDefId, | 606 | pub(crate) id: MacroDefId, |
655 | } | 607 | } |
656 | 608 | ||
657 | impl MacroDef {} | ||
658 | |||
659 | pub enum Container { | ||
660 | Trait(Trait), | ||
661 | ImplBlock(ImplBlock), | ||
662 | } | ||
663 | impl_froms!(Container: Trait, ImplBlock); | ||
664 | |||
665 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] | 609 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] |
666 | pub enum AssocItem { | 610 | pub enum AssocItem { |
667 | Function(Function), | 611 | Function(Function), |
@@ -810,7 +754,7 @@ impl ImplBlock { | |||
810 | let environment = TraitEnvironment::lower(db, &resolver); | 754 | let environment = TraitEnvironment::lower(db, &resolver); |
811 | let ty = Ty::from_hir(db, &resolver, &impl_data.target_type); | 755 | let ty = Ty::from_hir(db, &resolver, &impl_data.target_type); |
812 | Type { | 756 | Type { |
813 | krate: self.id.lookup(db).container.krate, | 757 | krate: self.id.lookup(db).container.module(db).krate, |
814 | ty: InEnvironment { value: ty, environment }, | 758 | ty: InEnvironment { value: ty, environment }, |
815 | } | 759 | } |
816 | } | 760 | } |
@@ -824,7 +768,7 @@ impl ImplBlock { | |||
824 | } | 768 | } |
825 | 769 | ||
826 | pub fn module(&self, db: &impl DefDatabase) -> Module { | 770 | pub fn module(&self, db: &impl DefDatabase) -> Module { |
827 | self.id.lookup(db).container.into() | 771 | self.id.lookup(db).container.module(db).into() |
828 | } | 772 | } |
829 | 773 | ||
830 | pub fn krate(&self, db: &impl DefDatabase) -> Crate { | 774 | pub fn krate(&self, db: &impl DefDatabase) -> Crate { |
diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 7abb4bd75..42ca55fe7 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs | |||
@@ -1,8 +1,10 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! Finds a corresponding hir data structure for a syntax node in a specific |
2 | //! file. | ||
3 | |||
2 | use hir_def::{ | 4 | use hir_def::{ |
3 | child_by_source::ChildBySource, dyn_map::DynMap, keys, keys::Key, nameres::ModuleSource, | 5 | child_by_source::ChildBySource, dyn_map::DynMap, keys, keys::Key, nameres::ModuleSource, |
4 | ConstId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, ModuleId, StaticId, StructId, | 6 | ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, ModuleId, |
5 | TraitId, TypeAliasId, UnionId, VariantId, | 7 | StaticId, StructId, TraitId, TypeAliasId, UnionId, VariantId, |
6 | }; | 8 | }; |
7 | use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind}; | 9 | use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind}; |
8 | use ra_syntax::{ | 10 | use ra_syntax::{ |
@@ -11,14 +13,14 @@ use ra_syntax::{ | |||
11 | }; | 13 | }; |
12 | 14 | ||
13 | use crate::{ | 15 | use crate::{ |
14 | db::{AstDatabase, DefDatabase, HirDatabase}, | 16 | db::{DefDatabase, HirDatabase}, |
15 | Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, ImplBlock, InFile, Local, | 17 | Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, ImplBlock, InFile, Local, |
16 | MacroDef, Module, Static, Struct, StructField, Trait, TypeAlias, TypeParam, Union, | 18 | MacroDef, Module, Static, Struct, StructField, Trait, TypeAlias, TypeParam, Union, |
17 | }; | 19 | }; |
18 | 20 | ||
19 | pub trait FromSource: Sized { | 21 | pub trait FromSource: Sized { |
20 | type Ast; | 22 | type Ast; |
21 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self>; | 23 | fn from_source(db: &impl DefDatabase, src: InFile<Self::Ast>) -> Option<Self>; |
22 | } | 24 | } |
23 | 25 | ||
24 | pub trait FromSourceByContainer: Sized { | 26 | pub trait FromSourceByContainer: Sized { |
@@ -32,7 +34,7 @@ where | |||
32 | T: From<<T as FromSourceByContainer>::Id>, | 34 | T: From<<T as FromSourceByContainer>::Id>, |
33 | { | 35 | { |
34 | type Ast = <T as FromSourceByContainer>::Ast; | 36 | type Ast = <T as FromSourceByContainer>::Ast; |
35 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { | 37 | fn from_source(db: &impl DefDatabase, src: InFile<Self::Ast>) -> Option<Self> { |
36 | analyze_container(db, src.as_ref().map(|it| it.syntax()))[T::KEY] | 38 | analyze_container(db, src.as_ref().map(|it| it.syntax()))[T::KEY] |
37 | .get(&src) | 39 | .get(&src) |
38 | .copied() | 40 | .copied() |
@@ -64,7 +66,7 @@ from_source_by_container_impls![ | |||
64 | 66 | ||
65 | impl FromSource for MacroDef { | 67 | impl FromSource for MacroDef { |
66 | type Ast = ast::MacroCall; | 68 | type Ast = ast::MacroCall; |
67 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { | 69 | fn from_source(db: &impl DefDatabase, src: InFile<Self::Ast>) -> Option<Self> { |
68 | let kind = MacroDefKind::Declarative; | 70 | let kind = MacroDefKind::Declarative; |
69 | 71 | ||
70 | let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax())); | 72 | let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax())); |
@@ -80,7 +82,7 @@ impl FromSource for MacroDef { | |||
80 | 82 | ||
81 | impl FromSource for EnumVariant { | 83 | impl FromSource for EnumVariant { |
82 | type Ast = ast::EnumVariant; | 84 | type Ast = ast::EnumVariant; |
83 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { | 85 | fn from_source(db: &impl DefDatabase, src: InFile<Self::Ast>) -> Option<Self> { |
84 | let parent_enum = src.value.parent_enum(); | 86 | let parent_enum = src.value.parent_enum(); |
85 | let src_enum = InFile { file_id: src.file_id, value: parent_enum }; | 87 | let src_enum = InFile { file_id: src.file_id, value: parent_enum }; |
86 | let parent_enum = Enum::from_source(db, src_enum)?; | 88 | let parent_enum = Enum::from_source(db, src_enum)?; |
@@ -93,7 +95,7 @@ impl FromSource for EnumVariant { | |||
93 | 95 | ||
94 | impl FromSource for StructField { | 96 | impl FromSource for StructField { |
95 | type Ast = FieldSource; | 97 | type Ast = FieldSource; |
96 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { | 98 | fn from_source(db: &impl DefDatabase, src: InFile<Self::Ast>) -> Option<Self> { |
97 | let src = src.as_ref(); | 99 | let src = src.as_ref(); |
98 | 100 | ||
99 | // FIXME this is buggy | 101 | // FIXME this is buggy |
@@ -212,29 +214,43 @@ impl Module { | |||
212 | } | 214 | } |
213 | 215 | ||
214 | fn analyze_container(db: &impl DefDatabase, src: InFile<&SyntaxNode>) -> DynMap { | 216 | fn analyze_container(db: &impl DefDatabase, src: InFile<&SyntaxNode>) -> DynMap { |
215 | _analyze_container(db, src).unwrap_or_default() | 217 | return child_by_source(db, src).unwrap_or_default(); |
216 | } | ||
217 | 218 | ||
218 | fn _analyze_container(db: &impl DefDatabase, src: InFile<&SyntaxNode>) -> Option<DynMap> { | 219 | fn child_by_source(db: &impl DefDatabase, src: InFile<&SyntaxNode>) -> Option<DynMap> { |
219 | // FIXME: this doesn't try to handle nested declarations | 220 | for container in src.value.ancestors().skip(1) { |
220 | for container in src.value.ancestors().skip(1) { | 221 | let res = match_ast! { |
221 | let res = match_ast! { | 222 | match container { |
222 | match container { | 223 | ast::TraitDef(it) => { |
223 | ast::TraitDef(it) => { | 224 | let def = Trait::from_source(db, src.with_value(it))?; |
224 | let c = Trait::from_source(db, src.with_value(it))?; | 225 | def.id.child_by_source(db) |
225 | c.id.child_by_source(db) | 226 | }, |
226 | }, | 227 | ast::ImplBlock(it) => { |
227 | ast::ImplBlock(it) => { | 228 | let def = ImplBlock::from_source(db, src.with_value(it))?; |
228 | let c = ImplBlock::from_source(db, src.with_value(it))?; | 229 | def.id.child_by_source(db) |
229 | c.id.child_by_source(db) | 230 | }, |
230 | }, | 231 | ast::FnDef(it) => { |
231 | _ => { continue }, | 232 | let def = Function::from_source(db, src.with_value(it))?; |
232 | } | 233 | DefWithBodyId::from(def.id) |
233 | }; | 234 | .child_by_source(db) |
234 | return Some(res); | 235 | }, |
235 | } | 236 | ast::StaticDef(it) => { |
237 | let def = Static::from_source(db, src.with_value(it))?; | ||
238 | DefWithBodyId::from(def.id) | ||
239 | .child_by_source(db) | ||
240 | }, | ||
241 | ast::ConstDef(it) => { | ||
242 | let def = Const::from_source(db, src.with_value(it))?; | ||
243 | DefWithBodyId::from(def.id) | ||
244 | .child_by_source(db) | ||
245 | }, | ||
246 | _ => { continue }, | ||
247 | } | ||
248 | }; | ||
249 | return Some(res); | ||
250 | } | ||
236 | 251 | ||
237 | let module_source = ModuleSource::from_child_node(db, src); | 252 | let module_source = ModuleSource::from_child_node(db, src); |
238 | let c = Module::from_definition(db, src.with_value(module_source))?; | 253 | let c = Module::from_definition(db, src.with_value(module_source))?; |
239 | Some(c.id.child_by_source(db)) | 254 | Some(c.id.child_by_source(db)) |
255 | } | ||
240 | } | 256 | } |
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 2e52a1f5c..7f9aef770 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs | |||
@@ -39,7 +39,7 @@ mod from_source; | |||
39 | 39 | ||
40 | pub use crate::{ | 40 | pub use crate::{ |
41 | code_model::{ | 41 | code_model::{ |
42 | Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency, DefWithBody, Docs, Enum, | 42 | Adt, AssocItem, AttrDef, Const, Crate, CrateDependency, DefWithBody, Docs, Enum, |
43 | EnumVariant, FieldSource, Function, GenericDef, HasAttrs, ImplBlock, Import, Local, | 43 | EnumVariant, FieldSource, Function, GenericDef, HasAttrs, ImplBlock, Import, Local, |
44 | MacroDef, Module, ModuleDef, ScopeDef, Static, Struct, StructField, Trait, Type, TypeAlias, | 44 | MacroDef, Module, ModuleDef, ScopeDef, Static, Struct, StructField, Trait, Type, TypeAlias, |
45 | TypeParam, Union, VariantDef, | 45 | TypeParam, Union, VariantDef, |
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index d326169b3..b60a6b87e 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs | |||
@@ -237,7 +237,13 @@ impl SourceAnalyzer { | |||
237 | } | 237 | } |
238 | 238 | ||
239 | pub fn resolve_record_field(&self, field: &ast::RecordField) -> Option<crate::StructField> { | 239 | pub fn resolve_record_field(&self, field: &ast::RecordField) -> Option<crate::StructField> { |
240 | let expr_id = self.expr_id(&field.expr()?)?; | 240 | let expr_id = match field.expr() { |
241 | Some(it) => self.expr_id(&it)?, | ||
242 | None => { | ||
243 | let src = InFile { file_id: self.file_id, value: field }; | ||
244 | self.body_source_map.as_ref()?.field_init_shorthand_expr(src)? | ||
245 | } | ||
246 | }; | ||
241 | self.infer.as_ref()?.record_field_resolution(expr_id).map(|it| it.into()) | 247 | self.infer.as_ref()?.record_field_resolution(expr_id).map(|it| it.into()) |
242 | } | 248 | } |
243 | 249 | ||