aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/body/lower.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def/src/body/lower.rs')
-rw-r--r--crates/ra_hir_def/src/body/lower.rs57
1 files changed, 55 insertions, 2 deletions
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs
index 61193b4d8..853e17bae 100644
--- a/crates/ra_hir_def/src/body/lower.rs
+++ b/crates/ra_hir_def/src/body/lower.rs
@@ -2,11 +2,12 @@
2//! representation. 2//! representation.
3 3
4use either::Either; 4use either::Either;
5
5use hir_expand::name::{name, AsName, Name}; 6use hir_expand::name::{name, AsName, Name};
6use ra_arena::Arena; 7use ra_arena::Arena;
7use ra_syntax::{ 8use ra_syntax::{
8 ast::{ 9 ast::{
9 self, ArgListOwner, ArrayExprKind, LiteralKind, LoopBodyOwner, NameOwner, 10 self, ArgListOwner, ArrayExprKind, LiteralKind, LoopBodyOwner, ModuleItemOwner, NameOwner,
10 TypeAscriptionOwner, 11 TypeAscriptionOwner,
11 }, 12 },
12 AstNode, AstPtr, 13 AstNode, AstPtr,
@@ -24,23 +25,28 @@ use crate::{
24 path::GenericArgs, 25 path::GenericArgs,
25 path::Path, 26 path::Path,
26 type_ref::{Mutability, TypeRef}, 27 type_ref::{Mutability, TypeRef},
28 ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, ModuleDefId, StaticLoc,
29 StructLoc, TraitLoc, TypeAliasLoc, UnionLoc,
27}; 30};
28 31
29pub(super) fn lower( 32pub(super) fn lower(
30 db: &impl DefDatabase, 33 db: &impl DefDatabase,
34 def: DefWithBodyId,
31 expander: Expander, 35 expander: Expander,
32 params: Option<ast::ParamList>, 36 params: Option<ast::ParamList>,
33 body: Option<ast::Expr>, 37 body: Option<ast::Expr>,
34) -> (Body, BodySourceMap) { 38) -> (Body, BodySourceMap) {
35 ExprCollector { 39 ExprCollector {
36 expander,
37 db, 40 db,
41 def,
42 expander,
38 source_map: BodySourceMap::default(), 43 source_map: BodySourceMap::default(),
39 body: Body { 44 body: Body {
40 exprs: Arena::default(), 45 exprs: Arena::default(),
41 pats: Arena::default(), 46 pats: Arena::default(),
42 params: Vec::new(), 47 params: Vec::new(),
43 body_expr: ExprId::dummy(), 48 body_expr: ExprId::dummy(),
49 defs: Vec::new(),
44 }, 50 },
45 } 51 }
46 .collect(params, body) 52 .collect(params, body)
@@ -48,6 +54,7 @@ pub(super) fn lower(
48 54
49struct ExprCollector<DB> { 55struct ExprCollector<DB> {
50 db: DB, 56 db: DB,
57 def: DefWithBodyId,
51 expander: Expander, 58 expander: Expander,
52 59
53 body: Body, 60 body: Body,
@@ -466,6 +473,7 @@ where
466 Some(block) => block, 473 Some(block) => block,
467 None => return self.alloc_expr(Expr::Missing, syntax_node_ptr), 474 None => return self.alloc_expr(Expr::Missing, syntax_node_ptr),
468 }; 475 };
476 self.collect_block_items(&block);
469 let statements = block 477 let statements = block
470 .statements() 478 .statements()
471 .map(|s| match s { 479 .map(|s| match s {
@@ -482,6 +490,51 @@ where
482 self.alloc_expr(Expr::Block { statements, tail }, syntax_node_ptr) 490 self.alloc_expr(Expr::Block { statements, tail }, syntax_node_ptr)
483 } 491 }
484 492
493 fn collect_block_items(&mut self, block: &ast::Block) {
494 let container = ContainerId::DefWithBodyId(self.def);
495 for item in block.items() {
496 let def: ModuleDefId = match item {
497 ast::ModuleItem::FnDef(def) => {
498 let ast_id = self.expander.ast_id(&def);
499 FunctionLoc { container: container.into(), ast_id }.intern(self.db).into()
500 }
501 ast::ModuleItem::TypeAliasDef(def) => {
502 let ast_id = self.expander.ast_id(&def);
503 TypeAliasLoc { container: container.into(), ast_id }.intern(self.db).into()
504 }
505 ast::ModuleItem::ConstDef(def) => {
506 let ast_id = self.expander.ast_id(&def);
507 ConstLoc { container: container.into(), ast_id }.intern(self.db).into()
508 }
509 ast::ModuleItem::StaticDef(def) => {
510 let ast_id = self.expander.ast_id(&def);
511 StaticLoc { container, ast_id }.intern(self.db).into()
512 }
513 ast::ModuleItem::StructDef(def) => {
514 let ast_id = self.expander.ast_id(&def);
515 StructLoc { container, ast_id }.intern(self.db).into()
516 }
517 ast::ModuleItem::EnumDef(def) => {
518 let ast_id = self.expander.ast_id(&def);
519 EnumLoc { container, ast_id }.intern(self.db).into()
520 }
521 ast::ModuleItem::UnionDef(def) => {
522 let ast_id = self.expander.ast_id(&def);
523 UnionLoc { container, ast_id }.intern(self.db).into()
524 }
525 ast::ModuleItem::TraitDef(def) => {
526 let ast_id = self.expander.ast_id(&def);
527 TraitLoc { container, ast_id }.intern(self.db).into()
528 }
529 ast::ModuleItem::ImplBlock(_)
530 | ast::ModuleItem::UseItem(_)
531 | ast::ModuleItem::ExternCrateItem(_)
532 | ast::ModuleItem::Module(_) => continue,
533 };
534 self.body.defs.push(def)
535 }
536 }
537
485 fn collect_block_opt(&mut self, expr: Option<ast::BlockExpr>) -> ExprId { 538 fn collect_block_opt(&mut self, expr: Option<ast::BlockExpr>) -> ExprId {
486 if let Some(block) = expr { 539 if let Some(block) = expr {
487 self.collect_block(block) 540 self.collect_block(block)