diff options
Diffstat (limited to 'crates/hir_def/src/body')
-rw-r--r-- | crates/hir_def/src/body/lower.rs | 157 |
1 files changed, 6 insertions, 151 deletions
diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index d4abe819d..8c8eb8007 100644 --- a/crates/hir_def/src/body/lower.rs +++ b/crates/hir_def/src/body/lower.rs | |||
@@ -1,17 +1,16 @@ | |||
1 | //! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr` | 1 | //! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr` |
2 | //! representation. | 2 | //! representation. |
3 | 3 | ||
4 | use std::{any::type_name, mem, sync::Arc}; | 4 | use std::mem; |
5 | 5 | ||
6 | use either::Either; | 6 | use either::Either; |
7 | use hir_expand::{ | 7 | use hir_expand::{ |
8 | hygiene::Hygiene, | 8 | hygiene::Hygiene, |
9 | name::{name, AsName, Name}, | 9 | name::{name, AsName, Name}, |
10 | ExpandError, HirFileId, MacroDefId, MacroDefKind, | 10 | ExpandError, HirFileId, |
11 | }; | 11 | }; |
12 | use la_arena::Arena; | 12 | use la_arena::Arena; |
13 | use profile::Count; | 13 | use profile::Count; |
14 | use rustc_hash::FxHashMap; | ||
15 | use syntax::{ | 14 | use syntax::{ |
16 | ast::{ | 15 | ast::{ |
17 | self, ArgListOwner, ArrayExprKind, AstChildren, LiteralKind, LoopBodyOwner, NameOwner, | 16 | self, ArgListOwner, ArrayExprKind, AstChildren, LiteralKind, LoopBodyOwner, NameOwner, |
@@ -32,11 +31,9 @@ use crate::{ | |||
32 | Statement, | 31 | Statement, |
33 | }, | 32 | }, |
34 | item_scope::BuiltinShadowMode, | 33 | item_scope::BuiltinShadowMode, |
35 | item_tree::{ItemTree, ItemTreeId, ItemTreeNode}, | ||
36 | path::{GenericArgs, Path}, | 34 | path::{GenericArgs, Path}, |
37 | type_ref::{Mutability, Rawness, TypeRef}, | 35 | type_ref::{Mutability, Rawness, TypeRef}, |
38 | AdtId, BlockLoc, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, | 36 | AdtId, BlockLoc, ModuleDefId, |
39 | ModuleDefId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, | ||
40 | }; | 37 | }; |
41 | 38 | ||
42 | use super::{diagnostics::BodyDiagnostic, ExprSource, PatSource}; | 39 | use super::{diagnostics::BodyDiagnostic, ExprSource, PatSource}; |
@@ -60,15 +57,12 @@ impl LowerCtx { | |||
60 | 57 | ||
61 | pub(super) fn lower( | 58 | pub(super) fn lower( |
62 | db: &dyn DefDatabase, | 59 | db: &dyn DefDatabase, |
63 | def: DefWithBodyId, | ||
64 | expander: Expander, | 60 | expander: Expander, |
65 | params: Option<ast::ParamList>, | 61 | params: Option<ast::ParamList>, |
66 | body: Option<ast::Expr>, | 62 | body: Option<ast::Expr>, |
67 | ) -> (Body, BodySourceMap) { | 63 | ) -> (Body, BodySourceMap) { |
68 | let item_tree = db.item_tree(expander.current_file_id); | ||
69 | ExprCollector { | 64 | ExprCollector { |
70 | db, | 65 | db, |
71 | def, | ||
72 | source_map: BodySourceMap::default(), | 66 | source_map: BodySourceMap::default(), |
73 | body: Body { | 67 | body: Body { |
74 | exprs: Arena::default(), | 68 | exprs: Arena::default(), |
@@ -76,14 +70,9 @@ pub(super) fn lower( | |||
76 | labels: Arena::default(), | 70 | labels: Arena::default(), |
77 | params: Vec::new(), | 71 | params: Vec::new(), |
78 | body_expr: dummy_expr_id(), | 72 | body_expr: dummy_expr_id(), |
79 | item_scope: Default::default(), | 73 | block_scopes: Vec::new(), |
80 | _c: Count::new(), | 74 | _c: Count::new(), |
81 | }, | 75 | }, |
82 | item_trees: { | ||
83 | let mut map = FxHashMap::default(); | ||
84 | map.insert(expander.current_file_id, item_tree); | ||
85 | map | ||
86 | }, | ||
87 | expander, | 76 | expander, |
88 | } | 77 | } |
89 | .collect(params, body) | 78 | .collect(params, body) |
@@ -91,12 +80,9 @@ pub(super) fn lower( | |||
91 | 80 | ||
92 | struct ExprCollector<'a> { | 81 | struct ExprCollector<'a> { |
93 | db: &'a dyn DefDatabase, | 82 | db: &'a dyn DefDatabase, |
94 | def: DefWithBodyId, | ||
95 | expander: Expander, | 83 | expander: Expander, |
96 | body: Body, | 84 | body: Body, |
97 | source_map: BodySourceMap, | 85 | source_map: BodySourceMap, |
98 | |||
99 | item_trees: FxHashMap<HirFileId, Arc<ItemTree>>, | ||
100 | } | 86 | } |
101 | 87 | ||
102 | impl ExprCollector<'_> { | 88 | impl ExprCollector<'_> { |
@@ -593,9 +579,6 @@ impl ExprCollector<'_> { | |||
593 | } else { | 579 | } else { |
594 | self.source_map.expansions.insert(macro_call, self.expander.current_file_id); | 580 | self.source_map.expansions.insert(macro_call, self.expander.current_file_id); |
595 | 581 | ||
596 | let item_tree = self.db.item_tree(self.expander.current_file_id); | ||
597 | self.item_trees.insert(self.expander.current_file_id, item_tree); | ||
598 | |||
599 | let id = collector(self, Some(expansion)); | 582 | let id = collector(self, Some(expansion)); |
600 | self.expander.exit(self.db, mark); | 583 | self.expander.exit(self.db, mark); |
601 | id | 584 | id |
@@ -605,32 +588,6 @@ impl ExprCollector<'_> { | |||
605 | } | 588 | } |
606 | } | 589 | } |
607 | 590 | ||
608 | fn find_inner_item<N: ItemTreeNode>(&self, ast: &N::Source) -> Option<ItemTreeId<N>> { | ||
609 | let id = self.expander.ast_id(ast); | ||
610 | let tree = &self.item_trees[&id.file_id]; | ||
611 | |||
612 | // FIXME: This probably breaks with `use` items, since they produce multiple item tree nodes | ||
613 | |||
614 | // Root file (non-macro). | ||
615 | let item_tree_id = tree | ||
616 | .all_inner_items() | ||
617 | .chain(tree.top_level_items().iter().copied()) | ||
618 | .filter_map(|mod_item| mod_item.downcast::<N>()) | ||
619 | .find(|tree_id| tree[*tree_id].ast_id().upcast() == id.value.upcast()) | ||
620 | .or_else(|| { | ||
621 | log::debug!( | ||
622 | "couldn't find inner {} item for {:?} (AST: `{}` - {:?})", | ||
623 | type_name::<N>(), | ||
624 | id, | ||
625 | ast.syntax(), | ||
626 | ast.syntax(), | ||
627 | ); | ||
628 | None | ||
629 | })?; | ||
630 | |||
631 | Some(ItemTreeId::new(id.file_id, item_tree_id)) | ||
632 | } | ||
633 | |||
634 | fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId { | 591 | fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId { |
635 | if let Some(expr) = expr { | 592 | if let Some(expr) = expr { |
636 | self.collect_expr(expr) | 593 | self.collect_expr(expr) |
@@ -662,7 +619,6 @@ impl ExprCollector<'_> { | |||
662 | match expansion { | 619 | match expansion { |
663 | Some(expansion) => { | 620 | Some(expansion) => { |
664 | let statements: ast::MacroStmts = expansion; | 621 | let statements: ast::MacroStmts = expansion; |
665 | this.collect_stmts_items(statements.statements()); | ||
666 | 622 | ||
667 | statements.statements().for_each(|stmt| { | 623 | statements.statements().for_each(|stmt| { |
668 | if let Some(mut r) = this.collect_stmt(stmt) { | 624 | if let Some(mut r) = this.collect_stmt(stmt) { |
@@ -700,6 +656,8 @@ impl ExprCollector<'_> { | |||
700 | let block_loc = | 656 | let block_loc = |
701 | BlockLoc { ast_id, module: self.expander.def_map.module_id(self.expander.module) }; | 657 | BlockLoc { ast_id, module: self.expander.def_map.module_id(self.expander.module) }; |
702 | let block_id = self.db.intern_block(block_loc); | 658 | let block_id = self.db.intern_block(block_loc); |
659 | self.body.block_scopes.push(block_id); | ||
660 | |||
703 | let opt_def_map = self.db.block_def_map(block_id); | 661 | let opt_def_map = self.db.block_def_map(block_id); |
704 | let has_def_map = opt_def_map.is_some(); | 662 | let has_def_map = opt_def_map.is_some(); |
705 | let def_map = opt_def_map.unwrap_or_else(|| self.expander.def_map.clone()); | 663 | let def_map = opt_def_map.unwrap_or_else(|| self.expander.def_map.clone()); |
@@ -707,7 +665,6 @@ impl ExprCollector<'_> { | |||
707 | let prev_def_map = mem::replace(&mut self.expander.def_map, def_map); | 665 | let prev_def_map = mem::replace(&mut self.expander.def_map, def_map); |
708 | let prev_local_module = mem::replace(&mut self.expander.module, module); | 666 | let prev_local_module = mem::replace(&mut self.expander.module, module); |
709 | 667 | ||
710 | self.collect_stmts_items(block.statements()); | ||
711 | let statements = | 668 | let statements = |
712 | block.statements().filter_map(|s| self.collect_stmt(s)).flatten().collect(); | 669 | block.statements().filter_map(|s| self.collect_stmt(s)).flatten().collect(); |
713 | let tail = block.tail_expr().map(|e| self.collect_expr(e)); | 670 | let tail = block.tail_expr().map(|e| self.collect_expr(e)); |
@@ -722,108 +679,6 @@ impl ExprCollector<'_> { | |||
722 | expr_id | 679 | expr_id |
723 | } | 680 | } |
724 | 681 | ||
725 | fn collect_stmts_items(&mut self, stmts: ast::AstChildren<ast::Stmt>) { | ||
726 | let container = ContainerId::DefWithBodyId(self.def); | ||
727 | |||
728 | let items = stmts | ||
729 | .filter_map(|stmt| match stmt { | ||
730 | ast::Stmt::Item(it) => Some(it), | ||
731 | ast::Stmt::LetStmt(_) | ast::Stmt::ExprStmt(_) => None, | ||
732 | }) | ||
733 | .filter_map(|item| { | ||
734 | let (def, name): (ModuleDefId, Option<ast::Name>) = match item { | ||
735 | ast::Item::Fn(def) => { | ||
736 | let id = self.find_inner_item(&def)?; | ||
737 | ( | ||
738 | FunctionLoc { container: container.into(), id }.intern(self.db).into(), | ||
739 | def.name(), | ||
740 | ) | ||
741 | } | ||
742 | ast::Item::TypeAlias(def) => { | ||
743 | let id = self.find_inner_item(&def)?; | ||
744 | ( | ||
745 | TypeAliasLoc { container: container.into(), id }.intern(self.db).into(), | ||
746 | def.name(), | ||
747 | ) | ||
748 | } | ||
749 | ast::Item::Const(def) => { | ||
750 | let id = self.find_inner_item(&def)?; | ||
751 | ( | ||
752 | ConstLoc { container: container.into(), id }.intern(self.db).into(), | ||
753 | def.name(), | ||
754 | ) | ||
755 | } | ||
756 | ast::Item::Static(def) => { | ||
757 | let id = self.find_inner_item(&def)?; | ||
758 | (StaticLoc { container, id }.intern(self.db).into(), def.name()) | ||
759 | } | ||
760 | ast::Item::Struct(def) => { | ||
761 | let id = self.find_inner_item(&def)?; | ||
762 | (StructLoc { container, id }.intern(self.db).into(), def.name()) | ||
763 | } | ||
764 | ast::Item::Enum(def) => { | ||
765 | let id = self.find_inner_item(&def)?; | ||
766 | (EnumLoc { container, id }.intern(self.db).into(), def.name()) | ||
767 | } | ||
768 | ast::Item::Union(def) => { | ||
769 | let id = self.find_inner_item(&def)?; | ||
770 | (UnionLoc { container, id }.intern(self.db).into(), def.name()) | ||
771 | } | ||
772 | ast::Item::Trait(def) => { | ||
773 | let id = self.find_inner_item(&def)?; | ||
774 | (TraitLoc { container, id }.intern(self.db).into(), def.name()) | ||
775 | } | ||
776 | ast::Item::ExternBlock(_) => return None, // FIXME: collect from extern blocks | ||
777 | ast::Item::Impl(_) | ||
778 | | ast::Item::Use(_) | ||
779 | | ast::Item::ExternCrate(_) | ||
780 | | ast::Item::Module(_) | ||
781 | | ast::Item::MacroCall(_) => return None, | ||
782 | ast::Item::MacroRules(def) => { | ||
783 | return Some(Either::Right(ast::Macro::from(def))); | ||
784 | } | ||
785 | ast::Item::MacroDef(def) => { | ||
786 | return Some(Either::Right(ast::Macro::from(def))); | ||
787 | } | ||
788 | }; | ||
789 | |||
790 | Some(Either::Left((def, name))) | ||
791 | }) | ||
792 | .collect::<Vec<_>>(); | ||
793 | |||
794 | for either in items { | ||
795 | match either { | ||
796 | Either::Left((def, name)) => { | ||
797 | self.body.item_scope.define_def(def); | ||
798 | if let Some(name) = name { | ||
799 | let vis = crate::visibility::Visibility::Public; // FIXME determine correctly | ||
800 | let has_constructor = match def { | ||
801 | ModuleDefId::AdtId(AdtId::StructId(s)) => { | ||
802 | self.db.struct_data(s).variant_data.kind() != StructKind::Record | ||
803 | } | ||
804 | _ => true, | ||
805 | }; | ||
806 | self.body.item_scope.push_res( | ||
807 | name.as_name(), | ||
808 | crate::per_ns::PerNs::from_def(def, vis, has_constructor), | ||
809 | ); | ||
810 | } | ||
811 | } | ||
812 | Either::Right(e) => { | ||
813 | let mac = MacroDefId { | ||
814 | krate: self.expander.def_map.krate(), | ||
815 | ast_id: Some(self.expander.ast_id(&e)), | ||
816 | kind: MacroDefKind::Declarative, | ||
817 | local_inner: false, | ||
818 | }; | ||
819 | if let Some(name) = e.name() { | ||
820 | self.body.item_scope.define_legacy_macro(name.as_name(), mac); | ||
821 | } | ||
822 | } | ||
823 | } | ||
824 | } | ||
825 | } | ||
826 | |||
827 | fn collect_block_opt(&mut self, expr: Option<ast::BlockExpr>) -> ExprId { | 682 | fn collect_block_opt(&mut self, expr: Option<ast::BlockExpr>) -> ExprId { |
828 | if let Some(block) = expr { | 683 | if let Some(block) = expr { |
829 | self.collect_block(block) | 684 | self.collect_block(block) |