diff options
| author | Aleksey Kladov <[email protected]> | 2019-11-12 15:46:57 +0000 |
|---|---|---|
| committer | Aleksey Kladov <[email protected]> | 2019-11-12 15:46:57 +0000 |
| commit | 1a90ad58023b065b7eecddf7f24417889a311850 (patch) | |
| tree | cf0cf77963257a4a2c6eb3bbaa74b0b90b457718 /crates | |
| parent | 55f3ff241a2105d2903266703474acbd24a85e84 (diff) | |
Move expression lowering to hir_def
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/ra_hir/src/db.rs | 4 | ||||
| -rw-r--r-- | crates/ra_hir/src/expr.rs | 166 | ||||
| -rw-r--r-- | crates/ra_hir/src/expr/lower.rs | 593 | ||||
| -rw-r--r-- | crates/ra_hir/src/marks.rs | 1 | ||||
| -rw-r--r-- | crates/ra_hir/src/ty/tests.rs | 3 | ||||
| -rw-r--r-- | crates/ra_hir_def/src/body.rs | 142 | ||||
| -rw-r--r-- | crates/ra_hir_def/src/body/lower.rs | 590 |
7 files changed, 767 insertions, 732 deletions
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index abf4ae402..9ac811232 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs | |||
| @@ -113,10 +113,10 @@ pub trait HirDatabase: DefDatabase + AstDatabase { | |||
| 113 | #[salsa::invoke(crate::ty::generic_defaults_query)] | 113 | #[salsa::invoke(crate::ty::generic_defaults_query)] |
| 114 | fn generic_defaults(&self, def: GenericDef) -> Substs; | 114 | fn generic_defaults(&self, def: GenericDef) -> Substs; |
| 115 | 115 | ||
| 116 | #[salsa::invoke(Body::body_with_source_map_query)] | 116 | #[salsa::invoke(crate::expr::body_with_source_map_query)] |
| 117 | fn body_with_source_map(&self, def: DefWithBody) -> (Arc<Body>, Arc<BodySourceMap>); | 117 | fn body_with_source_map(&self, def: DefWithBody) -> (Arc<Body>, Arc<BodySourceMap>); |
| 118 | 118 | ||
| 119 | #[salsa::invoke(Body::body_query)] | 119 | #[salsa::invoke(crate::expr::body_query)] |
| 120 | fn body(&self, def: DefWithBody) -> Arc<Body>; | 120 | fn body(&self, def: DefWithBody) -> Arc<Body>; |
| 121 | 121 | ||
| 122 | #[salsa::invoke(crate::ty::method_resolution::CrateImplBlocks::impls_in_crate_query)] | 122 | #[salsa::invoke(crate::ty::method_resolution::CrateImplBlocks::impls_in_crate_query)] |
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index ddf605111..82955fa55 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs | |||
| @@ -1,112 +1,52 @@ | |||
| 1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
| 2 | 2 | ||
| 3 | pub(crate) mod lower; | ||
| 4 | pub(crate) mod scope; | 3 | pub(crate) mod scope; |
| 5 | pub(crate) mod validation; | 4 | pub(crate) mod validation; |
| 6 | 5 | ||
| 7 | use std::{ops::Index, sync::Arc}; | 6 | use std::sync::Arc; |
| 8 | 7 | ||
| 9 | use ra_arena::{map::ArenaMap, Arena}; | ||
| 10 | use ra_syntax::{ast, AstPtr}; | 8 | use ra_syntax::{ast, AstPtr}; |
| 11 | use rustc_hash::FxHashMap; | ||
| 12 | 9 | ||
| 13 | use crate::{db::HirDatabase, DefWithBody, Either, HasSource, Resolver, Source}; | 10 | use crate::{db::HirDatabase, DefWithBody, HasSource, Resolver}; |
| 14 | 11 | ||
| 15 | pub use self::scope::ExprScopes; | 12 | pub use self::scope::ExprScopes; |
| 16 | 13 | ||
| 17 | pub use hir_def::expr::{ | 14 | pub use hir_def::{ |
| 18 | ArithOp, Array, BinaryOp, BindingAnnotation, CmpOp, Expr, ExprId, Literal, LogicOp, MatchArm, | 15 | body::{Body, BodySourceMap, ExprPtr, ExprSource, PatPtr, PatSource}, |
| 19 | Ordering, Pat, PatId, RecordFieldPat, RecordLitField, Statement, UnaryOp, | 16 | expr::{ |
| 17 | ArithOp, Array, BinaryOp, BindingAnnotation, CmpOp, Expr, ExprId, Literal, LogicOp, | ||
| 18 | MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, Statement, UnaryOp, | ||
| 19 | }, | ||
| 20 | }; | 20 | }; |
| 21 | 21 | ||
| 22 | /// The body of an item (function, const etc.). | 22 | pub(crate) fn body_with_source_map_query( |
| 23 | #[derive(Debug, Eq, PartialEq)] | 23 | db: &impl HirDatabase, |
| 24 | pub struct Body { | 24 | def: DefWithBody, |
| 25 | exprs: Arena<ExprId, Expr>, | 25 | ) -> (Arc<Body>, Arc<BodySourceMap>) { |
| 26 | pats: Arena<PatId, Pat>, | 26 | let mut params = None; |
| 27 | /// The patterns for the function's parameters. While the parameter types are | 27 | |
| 28 | /// part of the function signature, the patterns are not (they don't change | 28 | let (file_id, body) = match def { |
| 29 | /// the external type of the function). | 29 | DefWithBody::Function(f) => { |
| 30 | /// | 30 | let src = f.source(db); |
| 31 | /// If this `Body` is for the body of a constant, this will just be | 31 | params = src.ast.param_list(); |
| 32 | /// empty. | 32 | (src.file_id, src.ast.body().map(ast::Expr::from)) |
| 33 | params: Vec<PatId>, | 33 | } |
| 34 | /// The `ExprId` of the actual body expression. | 34 | DefWithBody::Const(c) => { |
| 35 | body_expr: ExprId, | 35 | let src = c.source(db); |
| 36 | (src.file_id, src.ast.body()) | ||
| 37 | } | ||
| 38 | DefWithBody::Static(s) => { | ||
| 39 | let src = s.source(db); | ||
| 40 | (src.file_id, src.ast.body()) | ||
| 41 | } | ||
| 42 | }; | ||
| 43 | let resolver = hir_def::body::MacroResolver::new(db, def.module(db).id); | ||
| 44 | let (body, source_map) = Body::new(db, resolver, file_id, params, body); | ||
| 45 | (Arc::new(body), Arc::new(source_map)) | ||
| 36 | } | 46 | } |
| 37 | 47 | ||
| 38 | type ExprPtr = Either<AstPtr<ast::Expr>, AstPtr<ast::RecordField>>; | 48 | pub(crate) fn body_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<Body> { |
| 39 | type ExprSource = Source<ExprPtr>; | 49 | db.body_with_source_map(def).0 |
| 40 | |||
| 41 | type PatPtr = Either<AstPtr<ast::Pat>, AstPtr<ast::SelfParam>>; | ||
| 42 | type PatSource = Source<PatPtr>; | ||
| 43 | |||
| 44 | /// An item body together with the mapping from syntax nodes to HIR expression | ||
| 45 | /// IDs. This is needed to go from e.g. a position in a file to the HIR | ||
| 46 | /// expression containing it; but for type inference etc., we want to operate on | ||
| 47 | /// a structure that is agnostic to the actual positions of expressions in the | ||
| 48 | /// file, so that we don't recompute types whenever some whitespace is typed. | ||
| 49 | /// | ||
| 50 | /// One complication here is that, due to macro expansion, a single `Body` might | ||
| 51 | /// be spread across several files. So, for each ExprId and PatId, we record | ||
| 52 | /// both the HirFileId and the position inside the file. However, we only store | ||
| 53 | /// AST -> ExprId mapping for non-macro files, as it is not clear how to handle | ||
| 54 | /// this properly for macros. | ||
| 55 | #[derive(Default, Debug, Eq, PartialEq)] | ||
| 56 | pub struct BodySourceMap { | ||
| 57 | expr_map: FxHashMap<ExprPtr, ExprId>, | ||
| 58 | expr_map_back: ArenaMap<ExprId, ExprSource>, | ||
| 59 | pat_map: FxHashMap<PatPtr, PatId>, | ||
| 60 | pat_map_back: ArenaMap<PatId, PatSource>, | ||
| 61 | field_map: FxHashMap<(ExprId, usize), AstPtr<ast::RecordField>>, | ||
| 62 | } | ||
| 63 | |||
| 64 | impl Body { | ||
| 65 | pub(crate) fn body_with_source_map_query( | ||
| 66 | db: &impl HirDatabase, | ||
| 67 | def: DefWithBody, | ||
| 68 | ) -> (Arc<Body>, Arc<BodySourceMap>) { | ||
| 69 | let mut params = None; | ||
| 70 | |||
| 71 | let (file_id, body) = match def { | ||
| 72 | DefWithBody::Function(f) => { | ||
| 73 | let src = f.source(db); | ||
| 74 | params = src.ast.param_list(); | ||
| 75 | (src.file_id, src.ast.body().map(ast::Expr::from)) | ||
| 76 | } | ||
| 77 | DefWithBody::Const(c) => { | ||
| 78 | let src = c.source(db); | ||
| 79 | (src.file_id, src.ast.body()) | ||
| 80 | } | ||
| 81 | DefWithBody::Static(s) => { | ||
| 82 | let src = s.source(db); | ||
| 83 | (src.file_id, src.ast.body()) | ||
| 84 | } | ||
| 85 | }; | ||
| 86 | |||
| 87 | let (body, source_map) = lower::lower(db, def.resolver(db), file_id, params, body); | ||
| 88 | (Arc::new(body), Arc::new(source_map)) | ||
| 89 | } | ||
| 90 | |||
| 91 | pub(crate) fn body_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<Body> { | ||
| 92 | db.body_with_source_map(def).0 | ||
| 93 | } | ||
| 94 | |||
| 95 | pub fn params(&self) -> &[PatId] { | ||
| 96 | &self.params | ||
| 97 | } | ||
| 98 | |||
| 99 | pub fn body_expr(&self) -> ExprId { | ||
| 100 | self.body_expr | ||
| 101 | } | ||
| 102 | |||
| 103 | pub fn exprs(&self) -> impl Iterator<Item = (ExprId, &Expr)> { | ||
| 104 | self.exprs.iter() | ||
| 105 | } | ||
| 106 | |||
| 107 | pub fn pats(&self) -> impl Iterator<Item = (PatId, &Pat)> { | ||
| 108 | self.pats.iter() | ||
| 109 | } | ||
| 110 | } | 50 | } |
| 111 | 51 | ||
| 112 | // needs arbitrary_self_types to be a method... or maybe move to the def? | 52 | // needs arbitrary_self_types to be a method... or maybe move to the def? |
| @@ -132,41 +72,3 @@ pub(crate) fn resolver_for_scope( | |||
| 132 | } | 72 | } |
| 133 | r | 73 | r |
| 134 | } | 74 | } |
| 135 | |||
| 136 | impl Index<ExprId> for Body { | ||
| 137 | type Output = Expr; | ||
| 138 | |||
| 139 | fn index(&self, expr: ExprId) -> &Expr { | ||
| 140 | &self.exprs[expr] | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | impl Index<PatId> for Body { | ||
| 145 | type Output = Pat; | ||
| 146 | |||
| 147 | fn index(&self, pat: PatId) -> &Pat { | ||
| 148 | &self.pats[pat] | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | impl BodySourceMap { | ||
| 153 | pub(crate) fn expr_syntax(&self, expr: ExprId) -> Option<ExprSource> { | ||
| 154 | self.expr_map_back.get(expr).copied() | ||
| 155 | } | ||
| 156 | |||
| 157 | pub(crate) fn node_expr(&self, node: &ast::Expr) -> Option<ExprId> { | ||
| 158 | self.expr_map.get(&Either::A(AstPtr::new(node))).cloned() | ||
| 159 | } | ||
| 160 | |||
| 161 | pub(crate) fn pat_syntax(&self, pat: PatId) -> Option<PatSource> { | ||
| 162 | self.pat_map_back.get(pat).copied() | ||
| 163 | } | ||
| 164 | |||
| 165 | pub(crate) fn node_pat(&self, node: &ast::Pat) -> Option<PatId> { | ||
| 166 | self.pat_map.get(&Either::A(AstPtr::new(node))).cloned() | ||
| 167 | } | ||
| 168 | |||
| 169 | pub(crate) fn field_syntax(&self, expr: ExprId, field: usize) -> AstPtr<ast::RecordField> { | ||
| 170 | self.field_map[&(expr, field)] | ||
| 171 | } | ||
| 172 | } | ||
diff --git a/crates/ra_hir/src/expr/lower.rs b/crates/ra_hir/src/expr/lower.rs deleted file mode 100644 index adc68b23c..000000000 --- a/crates/ra_hir/src/expr/lower.rs +++ /dev/null | |||
| @@ -1,593 +0,0 @@ | |||
| 1 | //! FIXME: write short doc here | ||
| 2 | |||
| 3 | use hir_def::{ | ||
| 4 | builtin_type::{BuiltinFloat, BuiltinInt}, | ||
| 5 | path::GenericArgs, | ||
| 6 | type_ref::TypeRef, | ||
| 7 | }; | ||
| 8 | use hir_expand::{ | ||
| 9 | hygiene::Hygiene, | ||
| 10 | name::{self, AsName, Name}, | ||
| 11 | }; | ||
| 12 | use ra_arena::Arena; | ||
| 13 | use ra_syntax::{ | ||
| 14 | ast::{ | ||
| 15 | self, ArgListOwner, ArrayExprKind, LiteralKind, LoopBodyOwner, NameOwner, | ||
| 16 | TypeAscriptionOwner, | ||
| 17 | }, | ||
| 18 | AstNode, AstPtr, | ||
| 19 | }; | ||
| 20 | use test_utils::tested_by; | ||
| 21 | |||
| 22 | use crate::{ | ||
| 23 | db::HirDatabase, AstId, Either, HirFileId, MacroCallLoc, MacroFileKind, Mutability, Path, | ||
| 24 | Resolver, Source, | ||
| 25 | }; | ||
| 26 | |||
| 27 | use super::{ | ||
| 28 | Array, BinaryOp, BindingAnnotation, Body, BodySourceMap, Expr, ExprId, Literal, MatchArm, Pat, | ||
| 29 | PatId, PatPtr, RecordFieldPat, RecordLitField, Statement, | ||
| 30 | }; | ||
| 31 | |||
| 32 | pub(super) fn lower( | ||
| 33 | db: &impl HirDatabase, | ||
| 34 | resolver: Resolver, | ||
| 35 | file_id: HirFileId, | ||
| 36 | params: Option<ast::ParamList>, | ||
| 37 | body: Option<ast::Expr>, | ||
| 38 | ) -> (Body, BodySourceMap) { | ||
| 39 | ExprCollector { | ||
| 40 | resolver, | ||
| 41 | db, | ||
| 42 | original_file_id: file_id, | ||
| 43 | current_file_id: file_id, | ||
| 44 | source_map: BodySourceMap::default(), | ||
| 45 | body: Body { | ||
| 46 | exprs: Arena::default(), | ||
| 47 | pats: Arena::default(), | ||
| 48 | params: Vec::new(), | ||
| 49 | body_expr: ExprId::dummy(), | ||
| 50 | }, | ||
| 51 | } | ||
| 52 | .collect(params, body) | ||
| 53 | } | ||
| 54 | |||
| 55 | struct ExprCollector<DB> { | ||
| 56 | db: DB, | ||
| 57 | resolver: Resolver, | ||
| 58 | // Expr collector expands macros along the way. original points to the file | ||
| 59 | // we started with, current points to the current macro expansion. source | ||
| 60 | // maps don't support macros yet, so we only record info into source map if | ||
| 61 | // current == original (see #1196) | ||
| 62 | original_file_id: HirFileId, | ||
| 63 | current_file_id: HirFileId, | ||
| 64 | |||
| 65 | body: Body, | ||
| 66 | source_map: BodySourceMap, | ||
| 67 | } | ||
| 68 | |||
| 69 | impl<'a, DB> ExprCollector<&'a DB> | ||
| 70 | where | ||
| 71 | DB: HirDatabase, | ||
| 72 | { | ||
| 73 | fn collect( | ||
| 74 | mut self, | ||
| 75 | param_list: Option<ast::ParamList>, | ||
| 76 | body: Option<ast::Expr>, | ||
| 77 | ) -> (Body, BodySourceMap) { | ||
| 78 | if let Some(param_list) = param_list { | ||
| 79 | if let Some(self_param) = param_list.self_param() { | ||
| 80 | let ptr = AstPtr::new(&self_param); | ||
| 81 | let param_pat = self.alloc_pat( | ||
| 82 | Pat::Bind { | ||
| 83 | name: name::SELF_PARAM, | ||
| 84 | mode: BindingAnnotation::Unannotated, | ||
| 85 | subpat: None, | ||
| 86 | }, | ||
| 87 | Either::B(ptr), | ||
| 88 | ); | ||
| 89 | self.body.params.push(param_pat); | ||
| 90 | } | ||
| 91 | |||
| 92 | for param in param_list.params() { | ||
| 93 | let pat = match param.pat() { | ||
| 94 | None => continue, | ||
| 95 | Some(pat) => pat, | ||
| 96 | }; | ||
| 97 | let param_pat = self.collect_pat(pat); | ||
| 98 | self.body.params.push(param_pat); | ||
| 99 | } | ||
| 100 | }; | ||
| 101 | |||
| 102 | self.body.body_expr = self.collect_expr_opt(body); | ||
| 103 | (self.body, self.source_map) | ||
| 104 | } | ||
| 105 | |||
| 106 | fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId { | ||
| 107 | let ptr = Either::A(ptr); | ||
| 108 | let id = self.body.exprs.alloc(expr); | ||
| 109 | if self.current_file_id == self.original_file_id { | ||
| 110 | self.source_map.expr_map.insert(ptr, id); | ||
| 111 | } | ||
| 112 | self.source_map | ||
| 113 | .expr_map_back | ||
| 114 | .insert(id, Source { file_id: self.current_file_id, ast: ptr }); | ||
| 115 | id | ||
| 116 | } | ||
| 117 | // desugared exprs don't have ptr, that's wrong and should be fixed | ||
| 118 | // somehow. | ||
| 119 | fn alloc_expr_desugared(&mut self, expr: Expr) -> ExprId { | ||
| 120 | self.body.exprs.alloc(expr) | ||
| 121 | } | ||
| 122 | fn alloc_expr_field_shorthand(&mut self, expr: Expr, ptr: AstPtr<ast::RecordField>) -> ExprId { | ||
| 123 | let ptr = Either::B(ptr); | ||
| 124 | let id = self.body.exprs.alloc(expr); | ||
| 125 | if self.current_file_id == self.original_file_id { | ||
| 126 | self.source_map.expr_map.insert(ptr, id); | ||
| 127 | } | ||
| 128 | self.source_map | ||
| 129 | .expr_map_back | ||
| 130 | .insert(id, Source { file_id: self.current_file_id, ast: ptr }); | ||
| 131 | id | ||
| 132 | } | ||
| 133 | fn alloc_pat(&mut self, pat: Pat, ptr: PatPtr) -> PatId { | ||
| 134 | let id = self.body.pats.alloc(pat); | ||
| 135 | if self.current_file_id == self.original_file_id { | ||
| 136 | self.source_map.pat_map.insert(ptr, id); | ||
| 137 | } | ||
| 138 | self.source_map.pat_map_back.insert(id, Source { file_id: self.current_file_id, ast: ptr }); | ||
| 139 | id | ||
| 140 | } | ||
| 141 | |||
| 142 | fn empty_block(&mut self) -> ExprId { | ||
| 143 | let block = Expr::Block { statements: Vec::new(), tail: None }; | ||
| 144 | self.body.exprs.alloc(block) | ||
| 145 | } | ||
| 146 | |||
| 147 | fn missing_expr(&mut self) -> ExprId { | ||
| 148 | self.body.exprs.alloc(Expr::Missing) | ||
| 149 | } | ||
| 150 | |||
| 151 | fn missing_pat(&mut self) -> PatId { | ||
| 152 | self.body.pats.alloc(Pat::Missing) | ||
| 153 | } | ||
| 154 | |||
| 155 | fn collect_expr(&mut self, expr: ast::Expr) -> ExprId { | ||
| 156 | let syntax_ptr = AstPtr::new(&expr); | ||
| 157 | match expr { | ||
| 158 | ast::Expr::IfExpr(e) => { | ||
| 159 | let then_branch = self.collect_block_opt(e.then_branch()); | ||
| 160 | |||
| 161 | let else_branch = e.else_branch().map(|b| match b { | ||
| 162 | ast::ElseBranch::Block(it) => self.collect_block(it), | ||
| 163 | ast::ElseBranch::IfExpr(elif) => { | ||
| 164 | let expr: ast::Expr = ast::Expr::cast(elif.syntax().clone()).unwrap(); | ||
| 165 | self.collect_expr(expr) | ||
| 166 | } | ||
| 167 | }); | ||
| 168 | |||
| 169 | let condition = match e.condition() { | ||
| 170 | None => self.missing_expr(), | ||
| 171 | Some(condition) => match condition.pat() { | ||
| 172 | None => self.collect_expr_opt(condition.expr()), | ||
| 173 | // if let -- desugar to match | ||
| 174 | Some(pat) => { | ||
| 175 | let pat = self.collect_pat(pat); | ||
| 176 | let match_expr = self.collect_expr_opt(condition.expr()); | ||
| 177 | let placeholder_pat = self.missing_pat(); | ||
| 178 | let arms = vec![ | ||
| 179 | MatchArm { pats: vec![pat], expr: then_branch, guard: None }, | ||
| 180 | MatchArm { | ||
| 181 | pats: vec![placeholder_pat], | ||
| 182 | expr: else_branch.unwrap_or_else(|| self.empty_block()), | ||
| 183 | guard: None, | ||
| 184 | }, | ||
| 185 | ]; | ||
| 186 | return self | ||
| 187 | .alloc_expr(Expr::Match { expr: match_expr, arms }, syntax_ptr); | ||
| 188 | } | ||
| 189 | }, | ||
| 190 | }; | ||
| 191 | |||
| 192 | self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr) | ||
| 193 | } | ||
| 194 | ast::Expr::TryBlockExpr(e) => { | ||
| 195 | let body = self.collect_block_opt(e.body()); | ||
| 196 | self.alloc_expr(Expr::TryBlock { body }, syntax_ptr) | ||
| 197 | } | ||
| 198 | ast::Expr::BlockExpr(e) => self.collect_block(e), | ||
| 199 | ast::Expr::LoopExpr(e) => { | ||
| 200 | let body = self.collect_block_opt(e.loop_body()); | ||
| 201 | self.alloc_expr(Expr::Loop { body }, syntax_ptr) | ||
| 202 | } | ||
| 203 | ast::Expr::WhileExpr(e) => { | ||
| 204 | let body = self.collect_block_opt(e.loop_body()); | ||
| 205 | |||
| 206 | let condition = match e.condition() { | ||
| 207 | None => self.missing_expr(), | ||
| 208 | Some(condition) => match condition.pat() { | ||
| 209 | None => self.collect_expr_opt(condition.expr()), | ||
| 210 | // if let -- desugar to match | ||
| 211 | Some(pat) => { | ||
| 212 | tested_by!(infer_while_let); | ||
| 213 | let pat = self.collect_pat(pat); | ||
| 214 | let match_expr = self.collect_expr_opt(condition.expr()); | ||
| 215 | let placeholder_pat = self.missing_pat(); | ||
| 216 | let break_ = self.alloc_expr_desugared(Expr::Break { expr: None }); | ||
| 217 | let arms = vec![ | ||
| 218 | MatchArm { pats: vec![pat], expr: body, guard: None }, | ||
| 219 | MatchArm { pats: vec![placeholder_pat], expr: break_, guard: None }, | ||
| 220 | ]; | ||
| 221 | let match_expr = | ||
| 222 | self.alloc_expr_desugared(Expr::Match { expr: match_expr, arms }); | ||
| 223 | return self.alloc_expr(Expr::Loop { body: match_expr }, syntax_ptr); | ||
| 224 | } | ||
| 225 | }, | ||
| 226 | }; | ||
| 227 | |||
| 228 | self.alloc_expr(Expr::While { condition, body }, syntax_ptr) | ||
| 229 | } | ||
| 230 | ast::Expr::ForExpr(e) => { | ||
| 231 | let iterable = self.collect_expr_opt(e.iterable()); | ||
| 232 | let pat = self.collect_pat_opt(e.pat()); | ||
| 233 | let body = self.collect_block_opt(e.loop_body()); | ||
| 234 | self.alloc_expr(Expr::For { iterable, pat, body }, syntax_ptr) | ||
| 235 | } | ||
| 236 | ast::Expr::CallExpr(e) => { | ||
| 237 | let callee = self.collect_expr_opt(e.expr()); | ||
| 238 | let args = if let Some(arg_list) = e.arg_list() { | ||
| 239 | arg_list.args().map(|e| self.collect_expr(e)).collect() | ||
| 240 | } else { | ||
| 241 | Vec::new() | ||
| 242 | }; | ||
| 243 | self.alloc_expr(Expr::Call { callee, args }, syntax_ptr) | ||
| 244 | } | ||
| 245 | ast::Expr::MethodCallExpr(e) => { | ||
| 246 | let receiver = self.collect_expr_opt(e.expr()); | ||
| 247 | let args = if let Some(arg_list) = e.arg_list() { | ||
| 248 | arg_list.args().map(|e| self.collect_expr(e)).collect() | ||
| 249 | } else { | ||
| 250 | Vec::new() | ||
| 251 | }; | ||
| 252 | let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); | ||
| 253 | let generic_args = e.type_arg_list().and_then(GenericArgs::from_ast); | ||
| 254 | self.alloc_expr( | ||
| 255 | Expr::MethodCall { receiver, method_name, args, generic_args }, | ||
| 256 | syntax_ptr, | ||
| 257 | ) | ||
| 258 | } | ||
| 259 | ast::Expr::MatchExpr(e) => { | ||
| 260 | let expr = self.collect_expr_opt(e.expr()); | ||
| 261 | let arms = if let Some(match_arm_list) = e.match_arm_list() { | ||
| 262 | match_arm_list | ||
| 263 | .arms() | ||
| 264 | .map(|arm| MatchArm { | ||
| 265 | pats: arm.pats().map(|p| self.collect_pat(p)).collect(), | ||
| 266 | expr: self.collect_expr_opt(arm.expr()), | ||
| 267 | guard: arm | ||
| 268 | .guard() | ||
| 269 | .and_then(|guard| guard.expr()) | ||
| 270 | .map(|e| self.collect_expr(e)), | ||
| 271 | }) | ||
| 272 | .collect() | ||
| 273 | } else { | ||
| 274 | Vec::new() | ||
| 275 | }; | ||
| 276 | self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr) | ||
| 277 | } | ||
| 278 | ast::Expr::PathExpr(e) => { | ||
| 279 | let path = e | ||
| 280 | .path() | ||
| 281 | .and_then(|path| self.parse_path(path)) | ||
| 282 | .map(Expr::Path) | ||
| 283 | .unwrap_or(Expr::Missing); | ||
| 284 | self.alloc_expr(path, syntax_ptr) | ||
| 285 | } | ||
| 286 | ast::Expr::ContinueExpr(_e) => { | ||
| 287 | // FIXME: labels | ||
| 288 | self.alloc_expr(Expr::Continue, syntax_ptr) | ||
| 289 | } | ||
| 290 | ast::Expr::BreakExpr(e) => { | ||
| 291 | let expr = e.expr().map(|e| self.collect_expr(e)); | ||
| 292 | self.alloc_expr(Expr::Break { expr }, syntax_ptr) | ||
| 293 | } | ||
| 294 | ast::Expr::ParenExpr(e) => { | ||
| 295 | let inner = self.collect_expr_opt(e.expr()); | ||
| 296 | // make the paren expr point to the inner expression as well | ||
| 297 | self.source_map.expr_map.insert(Either::A(syntax_ptr), inner); | ||
| 298 | inner | ||
| 299 | } | ||
| 300 | ast::Expr::ReturnExpr(e) => { | ||
| 301 | let expr = e.expr().map(|e| self.collect_expr(e)); | ||
| 302 | self.alloc_expr(Expr::Return { expr }, syntax_ptr) | ||
| 303 | } | ||
| 304 | ast::Expr::RecordLit(e) => { | ||
| 305 | let path = e.path().and_then(|path| self.parse_path(path)); | ||
| 306 | let mut field_ptrs = Vec::new(); | ||
| 307 | let record_lit = if let Some(nfl) = e.record_field_list() { | ||
| 308 | let fields = nfl | ||
| 309 | .fields() | ||
| 310 | .inspect(|field| field_ptrs.push(AstPtr::new(field))) | ||
| 311 | .map(|field| RecordLitField { | ||
| 312 | name: field | ||
| 313 | .name_ref() | ||
| 314 | .map(|nr| nr.as_name()) | ||
| 315 | .unwrap_or_else(Name::missing), | ||
| 316 | expr: if let Some(e) = field.expr() { | ||
| 317 | self.collect_expr(e) | ||
| 318 | } else if let Some(nr) = field.name_ref() { | ||
| 319 | // field shorthand | ||
| 320 | self.alloc_expr_field_shorthand( | ||
| 321 | Expr::Path(Path::from_name_ref(&nr)), | ||
| 322 | AstPtr::new(&field), | ||
| 323 | ) | ||
| 324 | } else { | ||
| 325 | self.missing_expr() | ||
| 326 | }, | ||
| 327 | }) | ||
| 328 | .collect(); | ||
| 329 | let spread = nfl.spread().map(|s| self.collect_expr(s)); | ||
| 330 | Expr::RecordLit { path, fields, spread } | ||
| 331 | } else { | ||
| 332 | Expr::RecordLit { path, fields: Vec::new(), spread: None } | ||
| 333 | }; | ||
| 334 | |||
| 335 | let res = self.alloc_expr(record_lit, syntax_ptr); | ||
| 336 | for (i, ptr) in field_ptrs.into_iter().enumerate() { | ||
| 337 | self.source_map.field_map.insert((res, i), ptr); | ||
| 338 | } | ||
| 339 | res | ||
| 340 | } | ||
| 341 | ast::Expr::FieldExpr(e) => { | ||
| 342 | let expr = self.collect_expr_opt(e.expr()); | ||
| 343 | let name = match e.field_access() { | ||
| 344 | Some(kind) => kind.as_name(), | ||
| 345 | _ => Name::missing(), | ||
| 346 | }; | ||
| 347 | self.alloc_expr(Expr::Field { expr, name }, syntax_ptr) | ||
| 348 | } | ||
| 349 | ast::Expr::AwaitExpr(e) => { | ||
| 350 | let expr = self.collect_expr_opt(e.expr()); | ||
| 351 | self.alloc_expr(Expr::Await { expr }, syntax_ptr) | ||
| 352 | } | ||
| 353 | ast::Expr::TryExpr(e) => { | ||
| 354 | let expr = self.collect_expr_opt(e.expr()); | ||
| 355 | self.alloc_expr(Expr::Try { expr }, syntax_ptr) | ||
| 356 | } | ||
| 357 | ast::Expr::CastExpr(e) => { | ||
| 358 | let expr = self.collect_expr_opt(e.expr()); | ||
| 359 | let type_ref = TypeRef::from_ast_opt(e.type_ref()); | ||
| 360 | self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr) | ||
| 361 | } | ||
| 362 | ast::Expr::RefExpr(e) => { | ||
| 363 | let expr = self.collect_expr_opt(e.expr()); | ||
| 364 | let mutability = Mutability::from_mutable(e.is_mut()); | ||
| 365 | self.alloc_expr(Expr::Ref { expr, mutability }, syntax_ptr) | ||
| 366 | } | ||
| 367 | ast::Expr::PrefixExpr(e) => { | ||
| 368 | let expr = self.collect_expr_opt(e.expr()); | ||
| 369 | if let Some(op) = e.op_kind() { | ||
| 370 | self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr) | ||
| 371 | } else { | ||
| 372 | self.alloc_expr(Expr::Missing, syntax_ptr) | ||
| 373 | } | ||
| 374 | } | ||
| 375 | ast::Expr::LambdaExpr(e) => { | ||
| 376 | let mut args = Vec::new(); | ||
| 377 | let mut arg_types = Vec::new(); | ||
| 378 | if let Some(pl) = e.param_list() { | ||
| 379 | for param in pl.params() { | ||
| 380 | let pat = self.collect_pat_opt(param.pat()); | ||
| 381 | let type_ref = param.ascribed_type().map(TypeRef::from_ast); | ||
| 382 | args.push(pat); | ||
| 383 | arg_types.push(type_ref); | ||
| 384 | } | ||
| 385 | } | ||
| 386 | let body = self.collect_expr_opt(e.body()); | ||
| 387 | self.alloc_expr(Expr::Lambda { args, arg_types, body }, syntax_ptr) | ||
| 388 | } | ||
| 389 | ast::Expr::BinExpr(e) => { | ||
| 390 | let lhs = self.collect_expr_opt(e.lhs()); | ||
| 391 | let rhs = self.collect_expr_opt(e.rhs()); | ||
| 392 | let op = e.op_kind().map(BinaryOp::from); | ||
| 393 | self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr) | ||
| 394 | } | ||
| 395 | ast::Expr::TupleExpr(e) => { | ||
| 396 | let exprs = e.exprs().map(|expr| self.collect_expr(expr)).collect(); | ||
| 397 | self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr) | ||
| 398 | } | ||
| 399 | ast::Expr::BoxExpr(e) => { | ||
| 400 | let expr = self.collect_expr_opt(e.expr()); | ||
| 401 | self.alloc_expr(Expr::Box { expr }, syntax_ptr) | ||
| 402 | } | ||
| 403 | |||
| 404 | ast::Expr::ArrayExpr(e) => { | ||
| 405 | let kind = e.kind(); | ||
| 406 | |||
| 407 | match kind { | ||
| 408 | ArrayExprKind::ElementList(e) => { | ||
| 409 | let exprs = e.map(|expr| self.collect_expr(expr)).collect(); | ||
| 410 | self.alloc_expr(Expr::Array(Array::ElementList(exprs)), syntax_ptr) | ||
| 411 | } | ||
| 412 | ArrayExprKind::Repeat { initializer, repeat } => { | ||
| 413 | let initializer = self.collect_expr_opt(initializer); | ||
| 414 | let repeat = self.collect_expr_opt(repeat); | ||
| 415 | self.alloc_expr( | ||
| 416 | Expr::Array(Array::Repeat { initializer, repeat }), | ||
| 417 | syntax_ptr, | ||
| 418 | ) | ||
| 419 | } | ||
| 420 | } | ||
| 421 | } | ||
| 422 | |||
| 423 | ast::Expr::Literal(e) => { | ||
| 424 | let lit = match e.kind() { | ||
| 425 | LiteralKind::IntNumber { suffix } => { | ||
| 426 | let known_name = suffix.and_then(|it| BuiltinInt::from_suffix(&it)); | ||
| 427 | |||
| 428 | Literal::Int(Default::default(), known_name) | ||
| 429 | } | ||
| 430 | LiteralKind::FloatNumber { suffix } => { | ||
| 431 | let known_name = suffix.and_then(|it| BuiltinFloat::from_suffix(&it)); | ||
| 432 | |||
| 433 | Literal::Float(Default::default(), known_name) | ||
| 434 | } | ||
| 435 | LiteralKind::ByteString => Literal::ByteString(Default::default()), | ||
| 436 | LiteralKind::String => Literal::String(Default::default()), | ||
| 437 | LiteralKind::Byte => Literal::Int(Default::default(), Some(BuiltinInt::U8)), | ||
| 438 | LiteralKind::Bool => Literal::Bool(Default::default()), | ||
| 439 | LiteralKind::Char => Literal::Char(Default::default()), | ||
| 440 | }; | ||
| 441 | self.alloc_expr(Expr::Literal(lit), syntax_ptr) | ||
| 442 | } | ||
| 443 | ast::Expr::IndexExpr(e) => { | ||
| 444 | let base = self.collect_expr_opt(e.base()); | ||
| 445 | let index = self.collect_expr_opt(e.index()); | ||
| 446 | self.alloc_expr(Expr::Index { base, index }, syntax_ptr) | ||
| 447 | } | ||
| 448 | |||
| 449 | // FIXME implement HIR for these: | ||
| 450 | ast::Expr::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), | ||
| 451 | ast::Expr::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), | ||
| 452 | ast::Expr::MacroCall(e) => { | ||
| 453 | let ast_id = AstId::new( | ||
| 454 | self.current_file_id, | ||
| 455 | self.db.ast_id_map(self.current_file_id).ast_id(&e), | ||
| 456 | ); | ||
| 457 | |||
| 458 | if let Some(path) = e.path().and_then(|path| self.parse_path(path)) { | ||
| 459 | if let Some(def) = self.resolver.resolve_path_as_macro(self.db, &path) { | ||
| 460 | let call_id = self.db.intern_macro(MacroCallLoc { def: def.id, ast_id }); | ||
| 461 | let file_id = call_id.as_file(MacroFileKind::Expr); | ||
| 462 | if let Some(node) = self.db.parse_or_expand(file_id) { | ||
| 463 | if let Some(expr) = ast::Expr::cast(node) { | ||
| 464 | log::debug!("macro expansion {:#?}", expr.syntax()); | ||
| 465 | let old_file_id = | ||
| 466 | std::mem::replace(&mut self.current_file_id, file_id); | ||
| 467 | let id = self.collect_expr(expr); | ||
| 468 | self.current_file_id = old_file_id; | ||
| 469 | return id; | ||
| 470 | } | ||
| 471 | } | ||
| 472 | } | ||
| 473 | } | ||
| 474 | // FIXME: Instead of just dropping the error from expansion | ||
| 475 | // report it | ||
| 476 | self.alloc_expr(Expr::Missing, syntax_ptr) | ||
| 477 | } | ||
| 478 | } | ||
| 479 | } | ||
| 480 | |||
| 481 | fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId { | ||
| 482 | if let Some(expr) = expr { | ||
| 483 | self.collect_expr(expr) | ||
| 484 | } else { | ||
| 485 | self.missing_expr() | ||
| 486 | } | ||
| 487 | } | ||
| 488 | |||
| 489 | fn collect_block(&mut self, expr: ast::BlockExpr) -> ExprId { | ||
| 490 | let syntax_node_ptr = AstPtr::new(&expr.clone().into()); | ||
| 491 | let block = match expr.block() { | ||
| 492 | Some(block) => block, | ||
| 493 | None => return self.alloc_expr(Expr::Missing, syntax_node_ptr), | ||
| 494 | }; | ||
| 495 | let statements = block | ||
| 496 | .statements() | ||
| 497 | .map(|s| match s { | ||
| 498 | ast::Stmt::LetStmt(stmt) => { | ||
| 499 | let pat = self.collect_pat_opt(stmt.pat()); | ||
| 500 | let type_ref = stmt.ascribed_type().map(TypeRef::from_ast); | ||
| 501 | let initializer = stmt.initializer().map(|e| self.collect_expr(e)); | ||
| 502 | Statement::Let { pat, type_ref, initializer } | ||
| 503 | } | ||
| 504 | ast::Stmt::ExprStmt(stmt) => Statement::Expr(self.collect_expr_opt(stmt.expr())), | ||
| 505 | }) | ||
| 506 | .collect(); | ||
| 507 | let tail = block.expr().map(|e| self.collect_expr(e)); | ||
| 508 | self.alloc_expr(Expr::Block { statements, tail }, syntax_node_ptr) | ||
| 509 | } | ||
| 510 | |||
| 511 | fn collect_block_opt(&mut self, expr: Option<ast::BlockExpr>) -> ExprId { | ||
| 512 | if let Some(block) = expr { | ||
| 513 | self.collect_block(block) | ||
| 514 | } else { | ||
| 515 | self.missing_expr() | ||
| 516 | } | ||
| 517 | } | ||
| 518 | |||
| 519 | fn collect_pat(&mut self, pat: ast::Pat) -> PatId { | ||
| 520 | let pattern = match &pat { | ||
| 521 | ast::Pat::BindPat(bp) => { | ||
| 522 | let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); | ||
| 523 | let annotation = BindingAnnotation::new(bp.is_mutable(), bp.is_ref()); | ||
| 524 | let subpat = bp.pat().map(|subpat| self.collect_pat(subpat)); | ||
| 525 | Pat::Bind { name, mode: annotation, subpat } | ||
| 526 | } | ||
| 527 | ast::Pat::TupleStructPat(p) => { | ||
| 528 | let path = p.path().and_then(|path| self.parse_path(path)); | ||
| 529 | let args = p.args().map(|p| self.collect_pat(p)).collect(); | ||
| 530 | Pat::TupleStruct { path, args } | ||
| 531 | } | ||
| 532 | ast::Pat::RefPat(p) => { | ||
| 533 | let pat = self.collect_pat_opt(p.pat()); | ||
| 534 | let mutability = Mutability::from_mutable(p.is_mut()); | ||
| 535 | Pat::Ref { pat, mutability } | ||
| 536 | } | ||
| 537 | ast::Pat::PathPat(p) => { | ||
| 538 | let path = p.path().and_then(|path| self.parse_path(path)); | ||
| 539 | path.map(Pat::Path).unwrap_or(Pat::Missing) | ||
| 540 | } | ||
| 541 | ast::Pat::TuplePat(p) => { | ||
| 542 | let args = p.args().map(|p| self.collect_pat(p)).collect(); | ||
| 543 | Pat::Tuple(args) | ||
| 544 | } | ||
| 545 | ast::Pat::PlaceholderPat(_) => Pat::Wild, | ||
| 546 | ast::Pat::RecordPat(p) => { | ||
| 547 | let path = p.path().and_then(|path| self.parse_path(path)); | ||
| 548 | let record_field_pat_list = | ||
| 549 | p.record_field_pat_list().expect("every struct should have a field list"); | ||
| 550 | let mut fields: Vec<_> = record_field_pat_list | ||
| 551 | .bind_pats() | ||
| 552 | .filter_map(|bind_pat| { | ||
| 553 | let ast_pat = | ||
| 554 | ast::Pat::cast(bind_pat.syntax().clone()).expect("bind pat is a pat"); | ||
| 555 | let pat = self.collect_pat(ast_pat); | ||
| 556 | let name = bind_pat.name()?.as_name(); | ||
| 557 | Some(RecordFieldPat { name, pat }) | ||
| 558 | }) | ||
| 559 | .collect(); | ||
| 560 | let iter = record_field_pat_list.record_field_pats().filter_map(|f| { | ||
| 561 | let ast_pat = f.pat()?; | ||
| 562 | let pat = self.collect_pat(ast_pat); | ||
| 563 | let name = f.name()?.as_name(); | ||
| 564 | Some(RecordFieldPat { name, pat }) | ||
| 565 | }); | ||
| 566 | fields.extend(iter); | ||
| 567 | |||
| 568 | Pat::Record { path, args: fields } | ||
| 569 | } | ||
| 570 | |||
| 571 | // FIXME: implement | ||
| 572 | ast::Pat::DotDotPat(_) => Pat::Missing, | ||
| 573 | ast::Pat::BoxPat(_) => Pat::Missing, | ||
| 574 | ast::Pat::LiteralPat(_) => Pat::Missing, | ||
| 575 | ast::Pat::SlicePat(_) | ast::Pat::RangePat(_) => Pat::Missing, | ||
| 576 | }; | ||
| 577 | let ptr = AstPtr::new(&pat); | ||
| 578 | self.alloc_pat(pattern, Either::A(ptr)) | ||
| 579 | } | ||
| 580 | |||
| 581 | fn collect_pat_opt(&mut self, pat: Option<ast::Pat>) -> PatId { | ||
| 582 | if let Some(pat) = pat { | ||
| 583 | self.collect_pat(pat) | ||
| 584 | } else { | ||
| 585 | self.missing_pat() | ||
| 586 | } | ||
| 587 | } | ||
| 588 | |||
| 589 | fn parse_path(&mut self, path: ast::Path) -> Option<Path> { | ||
| 590 | let hygiene = Hygiene::new(self.db, self.current_file_id); | ||
| 591 | Path::from_src(path, &hygiene) | ||
| 592 | } | ||
| 593 | } | ||
diff --git a/crates/ra_hir/src/marks.rs b/crates/ra_hir/src/marks.rs index 0d4fa5b67..0f754eb9c 100644 --- a/crates/ra_hir/src/marks.rs +++ b/crates/ra_hir/src/marks.rs | |||
| @@ -5,6 +5,5 @@ test_utils::marks!( | |||
| 5 | type_var_cycles_resolve_as_possible | 5 | type_var_cycles_resolve_as_possible |
| 6 | type_var_resolves_to_int_var | 6 | type_var_resolves_to_int_var |
| 7 | match_ergonomics_ref | 7 | match_ergonomics_ref |
| 8 | infer_while_let | ||
| 9 | coerce_merge_fail_fallback | 8 | coerce_merge_fail_fallback |
| 10 | ); | 9 | ); |
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 896bf2924..8863c3608 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs | |||
| @@ -222,7 +222,6 @@ mod collections { | |||
| 222 | 222 | ||
| 223 | #[test] | 223 | #[test] |
| 224 | fn infer_while_let() { | 224 | fn infer_while_let() { |
| 225 | covers!(infer_while_let); | ||
| 226 | let (db, pos) = TestDB::with_position( | 225 | let (db, pos) = TestDB::with_position( |
| 227 | r#" | 226 | r#" |
| 228 | //- /main.rs | 227 | //- /main.rs |
| @@ -4825,7 +4824,7 @@ fn main() { | |||
| 4825 | @r###" | 4824 | @r###" |
| 4826 | ![0; 1) '6': i32 | 4825 | ![0; 1) '6': i32 |
| 4827 | [64; 88) '{ ...!(); }': () | 4826 | [64; 88) '{ ...!(); }': () |
| 4828 | [74; 75) 'x': i32 | 4827 | [74; 75) 'x': i32 |
| 4829 | "### | 4828 | "### |
| 4830 | ); | 4829 | ); |
| 4831 | } | 4830 | } |
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index 7447904ea..ac8f8261b 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs | |||
| @@ -1,2 +1,144 @@ | |||
| 1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
| 2 | mod lower; | 2 | mod lower; |
| 3 | |||
| 4 | use std::{ops::Index, sync::Arc}; | ||
| 5 | |||
| 6 | use hir_expand::{either::Either, HirFileId, MacroDefId, Source}; | ||
| 7 | use ra_arena::{map::ArenaMap, Arena}; | ||
| 8 | use ra_syntax::{ast, AstPtr}; | ||
| 9 | use rustc_hash::FxHashMap; | ||
| 10 | |||
| 11 | use crate::{ | ||
| 12 | db::DefDatabase2, | ||
| 13 | expr::{Expr, ExprId, Pat, PatId}, | ||
| 14 | nameres::CrateDefMap, | ||
| 15 | path::Path, | ||
| 16 | ModuleId, | ||
| 17 | }; | ||
| 18 | |||
| 19 | pub struct MacroResolver { | ||
| 20 | crate_def_map: Arc<CrateDefMap>, | ||
| 21 | module: ModuleId, | ||
| 22 | } | ||
| 23 | |||
| 24 | impl MacroResolver { | ||
| 25 | pub fn new(db: &impl DefDatabase2, module: ModuleId) -> MacroResolver { | ||
| 26 | MacroResolver { crate_def_map: db.crate_def_map(module.krate), module } | ||
| 27 | } | ||
| 28 | |||
| 29 | pub(crate) fn resolve_path_as_macro( | ||
| 30 | &self, | ||
| 31 | db: &impl DefDatabase2, | ||
| 32 | path: &Path, | ||
| 33 | ) -> Option<MacroDefId> { | ||
| 34 | self.crate_def_map.resolve_path(db, self.module.module_id, path).0.get_macros() | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | /// The body of an item (function, const etc.). | ||
| 39 | #[derive(Debug, Eq, PartialEq)] | ||
| 40 | pub struct Body { | ||
| 41 | exprs: Arena<ExprId, Expr>, | ||
| 42 | pats: Arena<PatId, Pat>, | ||
| 43 | /// The patterns for the function's parameters. While the parameter types are | ||
| 44 | /// part of the function signature, the patterns are not (they don't change | ||
| 45 | /// the external type of the function). | ||
| 46 | /// | ||
| 47 | /// If this `Body` is for the body of a constant, this will just be | ||
| 48 | /// empty. | ||
| 49 | params: Vec<PatId>, | ||
| 50 | /// The `ExprId` of the actual body expression. | ||
| 51 | body_expr: ExprId, | ||
| 52 | } | ||
| 53 | |||
| 54 | pub type ExprPtr = Either<AstPtr<ast::Expr>, AstPtr<ast::RecordField>>; | ||
| 55 | pub type ExprSource = Source<ExprPtr>; | ||
| 56 | |||
| 57 | pub type PatPtr = Either<AstPtr<ast::Pat>, AstPtr<ast::SelfParam>>; | ||
| 58 | pub type PatSource = Source<PatPtr>; | ||
| 59 | |||
| 60 | /// An item body together with the mapping from syntax nodes to HIR expression | ||
| 61 | /// IDs. This is needed to go from e.g. a position in a file to the HIR | ||
| 62 | /// expression containing it; but for type inference etc., we want to operate on | ||
| 63 | /// a structure that is agnostic to the actual positions of expressions in the | ||
| 64 | /// file, so that we don't recompute types whenever some whitespace is typed. | ||
| 65 | /// | ||
| 66 | /// One complication here is that, due to macro expansion, a single `Body` might | ||
| 67 | /// be spread across several files. So, for each ExprId and PatId, we record | ||
| 68 | /// both the HirFileId and the position inside the file. However, we only store | ||
| 69 | /// AST -> ExprId mapping for non-macro files, as it is not clear how to handle | ||
| 70 | /// this properly for macros. | ||
| 71 | #[derive(Default, Debug, Eq, PartialEq)] | ||
| 72 | pub struct BodySourceMap { | ||
| 73 | expr_map: FxHashMap<ExprPtr, ExprId>, | ||
| 74 | expr_map_back: ArenaMap<ExprId, ExprSource>, | ||
| 75 | pat_map: FxHashMap<PatPtr, PatId>, | ||
| 76 | pat_map_back: ArenaMap<PatId, PatSource>, | ||
| 77 | field_map: FxHashMap<(ExprId, usize), AstPtr<ast::RecordField>>, | ||
| 78 | } | ||
| 79 | |||
| 80 | impl Body { | ||
| 81 | pub fn new( | ||
| 82 | db: &impl DefDatabase2, | ||
| 83 | resolver: MacroResolver, | ||
| 84 | file_id: HirFileId, | ||
| 85 | params: Option<ast::ParamList>, | ||
| 86 | body: Option<ast::Expr>, | ||
| 87 | ) -> (Body, BodySourceMap) { | ||
| 88 | lower::lower(db, resolver, file_id, params, body) | ||
| 89 | } | ||
| 90 | |||
| 91 | pub fn params(&self) -> &[PatId] { | ||
| 92 | &self.params | ||
| 93 | } | ||
| 94 | |||
| 95 | pub fn body_expr(&self) -> ExprId { | ||
| 96 | self.body_expr | ||
| 97 | } | ||
| 98 | |||
| 99 | pub fn exprs(&self) -> impl Iterator<Item = (ExprId, &Expr)> { | ||
| 100 | self.exprs.iter() | ||
| 101 | } | ||
| 102 | |||
| 103 | pub fn pats(&self) -> impl Iterator<Item = (PatId, &Pat)> { | ||
| 104 | self.pats.iter() | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | impl Index<ExprId> for Body { | ||
| 109 | type Output = Expr; | ||
| 110 | |||
| 111 | fn index(&self, expr: ExprId) -> &Expr { | ||
| 112 | &self.exprs[expr] | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | impl Index<PatId> for Body { | ||
| 117 | type Output = Pat; | ||
| 118 | |||
| 119 | fn index(&self, pat: PatId) -> &Pat { | ||
| 120 | &self.pats[pat] | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | impl BodySourceMap { | ||
| 125 | pub fn expr_syntax(&self, expr: ExprId) -> Option<ExprSource> { | ||
| 126 | self.expr_map_back.get(expr).copied() | ||
| 127 | } | ||
| 128 | |||
| 129 | pub fn node_expr(&self, node: &ast::Expr) -> Option<ExprId> { | ||
| 130 | self.expr_map.get(&Either::A(AstPtr::new(node))).cloned() | ||
| 131 | } | ||
| 132 | |||
| 133 | pub fn pat_syntax(&self, pat: PatId) -> Option<PatSource> { | ||
| 134 | self.pat_map_back.get(pat).copied() | ||
| 135 | } | ||
| 136 | |||
| 137 | pub fn node_pat(&self, node: &ast::Pat) -> Option<PatId> { | ||
| 138 | self.pat_map.get(&Either::A(AstPtr::new(node))).cloned() | ||
| 139 | } | ||
| 140 | |||
| 141 | pub fn field_syntax(&self, expr: ExprId, field: usize) -> AstPtr<ast::RecordField> { | ||
| 142 | self.field_map[&(expr, field)] | ||
| 143 | } | ||
| 144 | } | ||
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 1a144b1f9..1f93260d6 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs | |||
| @@ -1,8 +1,594 @@ | |||
| 1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
| 2 | 2 | ||
| 3 | use ra_syntax::ast; | 3 | use hir_expand::{ |
| 4 | either::Either, | ||
| 5 | hygiene::Hygiene, | ||
| 6 | name::{self, AsName, Name}, | ||
| 7 | AstId, HirFileId, MacroCallLoc, MacroFileKind, Source, | ||
| 8 | }; | ||
| 9 | use ra_arena::Arena; | ||
| 10 | use ra_syntax::{ | ||
| 11 | ast::{ | ||
| 12 | self, ArgListOwner, ArrayExprKind, LiteralKind, LoopBodyOwner, NameOwner, | ||
| 13 | TypeAscriptionOwner, | ||
| 14 | }, | ||
| 15 | AstNode, AstPtr, | ||
| 16 | }; | ||
| 4 | 17 | ||
| 5 | use crate::expr::{ArithOp, BinaryOp, CmpOp, LogicOp, Ordering}; | 18 | use crate::{ |
| 19 | body::{Body, BodySourceMap, MacroResolver, PatPtr}, | ||
| 20 | builtin_type::{BuiltinFloat, BuiltinInt}, | ||
| 21 | db::DefDatabase2, | ||
| 22 | expr::{ | ||
| 23 | ArithOp, Array, BinaryOp, BindingAnnotation, CmpOp, Expr, ExprId, Literal, LogicOp, | ||
| 24 | MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, Statement, | ||
| 25 | }, | ||
| 26 | path::GenericArgs, | ||
| 27 | path::Path, | ||
| 28 | type_ref::{Mutability, TypeRef}, | ||
| 29 | }; | ||
| 30 | |||
| 31 | pub(super) fn lower( | ||
| 32 | db: &impl DefDatabase2, | ||
| 33 | resolver: MacroResolver, | ||
| 34 | file_id: HirFileId, | ||
| 35 | params: Option<ast::ParamList>, | ||
| 36 | body: Option<ast::Expr>, | ||
| 37 | ) -> (Body, BodySourceMap) { | ||
| 38 | ExprCollector { | ||
| 39 | resolver, | ||
| 40 | db, | ||
| 41 | original_file_id: file_id, | ||
| 42 | current_file_id: file_id, | ||
| 43 | source_map: BodySourceMap::default(), | ||
| 44 | body: Body { | ||
| 45 | exprs: Arena::default(), | ||
| 46 | pats: Arena::default(), | ||
| 47 | params: Vec::new(), | ||
| 48 | body_expr: ExprId::dummy(), | ||
| 49 | }, | ||
| 50 | } | ||
| 51 | .collect(params, body) | ||
| 52 | } | ||
| 53 | |||
| 54 | struct ExprCollector<DB> { | ||
| 55 | db: DB, | ||
| 56 | resolver: MacroResolver, | ||
| 57 | // Expr collector expands macros along the way. original points to the file | ||
| 58 | // we started with, current points to the current macro expansion. source | ||
| 59 | // maps don't support macros yet, so we only record info into source map if | ||
| 60 | // current == original (see #1196) | ||
| 61 | original_file_id: HirFileId, | ||
| 62 | current_file_id: HirFileId, | ||
| 63 | |||
| 64 | body: Body, | ||
| 65 | source_map: BodySourceMap, | ||
| 66 | } | ||
| 67 | |||
| 68 | impl<'a, DB> ExprCollector<&'a DB> | ||
| 69 | where | ||
| 70 | DB: DefDatabase2, | ||
| 71 | { | ||
| 72 | fn collect( | ||
| 73 | mut self, | ||
| 74 | param_list: Option<ast::ParamList>, | ||
| 75 | body: Option<ast::Expr>, | ||
| 76 | ) -> (Body, BodySourceMap) { | ||
| 77 | if let Some(param_list) = param_list { | ||
| 78 | if let Some(self_param) = param_list.self_param() { | ||
| 79 | let ptr = AstPtr::new(&self_param); | ||
| 80 | let param_pat = self.alloc_pat( | ||
| 81 | Pat::Bind { | ||
| 82 | name: name::SELF_PARAM, | ||
| 83 | mode: BindingAnnotation::Unannotated, | ||
| 84 | subpat: None, | ||
| 85 | }, | ||
| 86 | Either::B(ptr), | ||
| 87 | ); | ||
| 88 | self.body.params.push(param_pat); | ||
| 89 | } | ||
| 90 | |||
| 91 | for param in param_list.params() { | ||
| 92 | let pat = match param.pat() { | ||
| 93 | None => continue, | ||
| 94 | Some(pat) => pat, | ||
| 95 | }; | ||
| 96 | let param_pat = self.collect_pat(pat); | ||
| 97 | self.body.params.push(param_pat); | ||
| 98 | } | ||
| 99 | }; | ||
| 100 | |||
| 101 | self.body.body_expr = self.collect_expr_opt(body); | ||
| 102 | (self.body, self.source_map) | ||
| 103 | } | ||
| 104 | |||
| 105 | fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId { | ||
| 106 | let ptr = Either::A(ptr); | ||
| 107 | let id = self.body.exprs.alloc(expr); | ||
| 108 | if self.current_file_id == self.original_file_id { | ||
| 109 | self.source_map.expr_map.insert(ptr, id); | ||
| 110 | } | ||
| 111 | self.source_map | ||
| 112 | .expr_map_back | ||
| 113 | .insert(id, Source { file_id: self.current_file_id, ast: ptr }); | ||
| 114 | id | ||
| 115 | } | ||
| 116 | // desugared exprs don't have ptr, that's wrong and should be fixed | ||
| 117 | // somehow. | ||
| 118 | fn alloc_expr_desugared(&mut self, expr: Expr) -> ExprId { | ||
| 119 | self.body.exprs.alloc(expr) | ||
| 120 | } | ||
| 121 | fn alloc_expr_field_shorthand(&mut self, expr: Expr, ptr: AstPtr<ast::RecordField>) -> ExprId { | ||
| 122 | let ptr = Either::B(ptr); | ||
| 123 | let id = self.body.exprs.alloc(expr); | ||
| 124 | if self.current_file_id == self.original_file_id { | ||
| 125 | self.source_map.expr_map.insert(ptr, id); | ||
| 126 | } | ||
| 127 | self.source_map | ||
| 128 | .expr_map_back | ||
| 129 | .insert(id, Source { file_id: self.current_file_id, ast: ptr }); | ||
| 130 | id | ||
| 131 | } | ||
| 132 | fn alloc_pat(&mut self, pat: Pat, ptr: PatPtr) -> PatId { | ||
| 133 | let id = self.body.pats.alloc(pat); | ||
| 134 | if self.current_file_id == self.original_file_id { | ||
| 135 | self.source_map.pat_map.insert(ptr, id); | ||
| 136 | } | ||
| 137 | self.source_map.pat_map_back.insert(id, Source { file_id: self.current_file_id, ast: ptr }); | ||
| 138 | id | ||
| 139 | } | ||
| 140 | |||
| 141 | fn empty_block(&mut self) -> ExprId { | ||
| 142 | let block = Expr::Block { statements: Vec::new(), tail: None }; | ||
| 143 | self.body.exprs.alloc(block) | ||
| 144 | } | ||
| 145 | |||
| 146 | fn missing_expr(&mut self) -> ExprId { | ||
| 147 | self.body.exprs.alloc(Expr::Missing) | ||
| 148 | } | ||
| 149 | |||
| 150 | fn missing_pat(&mut self) -> PatId { | ||
| 151 | self.body.pats.alloc(Pat::Missing) | ||
| 152 | } | ||
| 153 | |||
| 154 | fn collect_expr(&mut self, expr: ast::Expr) -> ExprId { | ||
| 155 | let syntax_ptr = AstPtr::new(&expr); | ||
| 156 | match expr { | ||
| 157 | ast::Expr::IfExpr(e) => { | ||
| 158 | let then_branch = self.collect_block_opt(e.then_branch()); | ||
| 159 | |||
| 160 | let else_branch = e.else_branch().map(|b| match b { | ||
| 161 | ast::ElseBranch::Block(it) => self.collect_block(it), | ||
| 162 | ast::ElseBranch::IfExpr(elif) => { | ||
| 163 | let expr: ast::Expr = ast::Expr::cast(elif.syntax().clone()).unwrap(); | ||
| 164 | self.collect_expr(expr) | ||
| 165 | } | ||
| 166 | }); | ||
| 167 | |||
| 168 | let condition = match e.condition() { | ||
| 169 | None => self.missing_expr(), | ||
| 170 | Some(condition) => match condition.pat() { | ||
| 171 | None => self.collect_expr_opt(condition.expr()), | ||
| 172 | // if let -- desugar to match | ||
| 173 | Some(pat) => { | ||
| 174 | let pat = self.collect_pat(pat); | ||
| 175 | let match_expr = self.collect_expr_opt(condition.expr()); | ||
| 176 | let placeholder_pat = self.missing_pat(); | ||
| 177 | let arms = vec![ | ||
| 178 | MatchArm { pats: vec![pat], expr: then_branch, guard: None }, | ||
| 179 | MatchArm { | ||
| 180 | pats: vec![placeholder_pat], | ||
| 181 | expr: else_branch.unwrap_or_else(|| self.empty_block()), | ||
| 182 | guard: None, | ||
| 183 | }, | ||
| 184 | ]; | ||
| 185 | return self | ||
| 186 | .alloc_expr(Expr::Match { expr: match_expr, arms }, syntax_ptr); | ||
| 187 | } | ||
| 188 | }, | ||
| 189 | }; | ||
| 190 | |||
| 191 | self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr) | ||
| 192 | } | ||
| 193 | ast::Expr::TryBlockExpr(e) => { | ||
| 194 | let body = self.collect_block_opt(e.body()); | ||
| 195 | self.alloc_expr(Expr::TryBlock { body }, syntax_ptr) | ||
| 196 | } | ||
| 197 | ast::Expr::BlockExpr(e) => self.collect_block(e), | ||
| 198 | ast::Expr::LoopExpr(e) => { | ||
| 199 | let body = self.collect_block_opt(e.loop_body()); | ||
| 200 | self.alloc_expr(Expr::Loop { body }, syntax_ptr) | ||
| 201 | } | ||
| 202 | ast::Expr::WhileExpr(e) => { | ||
| 203 | let body = self.collect_block_opt(e.loop_body()); | ||
| 204 | |||
| 205 | let condition = match e.condition() { | ||
| 206 | None => self.missing_expr(), | ||
| 207 | Some(condition) => match condition.pat() { | ||
| 208 | None => self.collect_expr_opt(condition.expr()), | ||
| 209 | // if let -- desugar to match | ||
| 210 | Some(pat) => { | ||
| 211 | let pat = self.collect_pat(pat); | ||
| 212 | let match_expr = self.collect_expr_opt(condition.expr()); | ||
| 213 | let placeholder_pat = self.missing_pat(); | ||
| 214 | let break_ = self.alloc_expr_desugared(Expr::Break { expr: None }); | ||
| 215 | let arms = vec![ | ||
| 216 | MatchArm { pats: vec![pat], expr: body, guard: None }, | ||
| 217 | MatchArm { pats: vec![placeholder_pat], expr: break_, guard: None }, | ||
| 218 | ]; | ||
| 219 | let match_expr = | ||
| 220 | self.alloc_expr_desugared(Expr::Match { expr: match_expr, arms }); | ||
| 221 | return self.alloc_expr(Expr::Loop { body: match_expr }, syntax_ptr); | ||
| 222 | } | ||
| 223 | }, | ||
| 224 | }; | ||
| 225 | |||
| 226 | self.alloc_expr(Expr::While { condition, body }, syntax_ptr) | ||
| 227 | } | ||
| 228 | ast::Expr::ForExpr(e) => { | ||
| 229 | let iterable = self.collect_expr_opt(e.iterable()); | ||
| 230 | let pat = self.collect_pat_opt(e.pat()); | ||
| 231 | let body = self.collect_block_opt(e.loop_body()); | ||
| 232 | self.alloc_expr(Expr::For { iterable, pat, body }, syntax_ptr) | ||
| 233 | } | ||
| 234 | ast::Expr::CallExpr(e) => { | ||
| 235 | let callee = self.collect_expr_opt(e.expr()); | ||
| 236 | let args = if let Some(arg_list) = e.arg_list() { | ||
| 237 | arg_list.args().map(|e| self.collect_expr(e)).collect() | ||
| 238 | } else { | ||
| 239 | Vec::new() | ||
| 240 | }; | ||
| 241 | self.alloc_expr(Expr::Call { callee, args }, syntax_ptr) | ||
| 242 | } | ||
| 243 | ast::Expr::MethodCallExpr(e) => { | ||
| 244 | let receiver = self.collect_expr_opt(e.expr()); | ||
| 245 | let args = if let Some(arg_list) = e.arg_list() { | ||
| 246 | arg_list.args().map(|e| self.collect_expr(e)).collect() | ||
| 247 | } else { | ||
| 248 | Vec::new() | ||
| 249 | }; | ||
| 250 | let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); | ||
| 251 | let generic_args = e.type_arg_list().and_then(GenericArgs::from_ast); | ||
| 252 | self.alloc_expr( | ||
| 253 | Expr::MethodCall { receiver, method_name, args, generic_args }, | ||
| 254 | syntax_ptr, | ||
| 255 | ) | ||
| 256 | } | ||
| 257 | ast::Expr::MatchExpr(e) => { | ||
| 258 | let expr = self.collect_expr_opt(e.expr()); | ||
| 259 | let arms = if let Some(match_arm_list) = e.match_arm_list() { | ||
| 260 | match_arm_list | ||
| 261 | .arms() | ||
| 262 | .map(|arm| MatchArm { | ||
| 263 | pats: arm.pats().map(|p| self.collect_pat(p)).collect(), | ||
| 264 | expr: self.collect_expr_opt(arm.expr()), | ||
| 265 | guard: arm | ||
| 266 | .guard() | ||
| 267 | .and_then(|guard| guard.expr()) | ||
| 268 | .map(|e| self.collect_expr(e)), | ||
| 269 | }) | ||
| 270 | .collect() | ||
| 271 | } else { | ||
| 272 | Vec::new() | ||
| 273 | }; | ||
| 274 | self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr) | ||
| 275 | } | ||
| 276 | ast::Expr::PathExpr(e) => { | ||
| 277 | let path = e | ||
| 278 | .path() | ||
| 279 | .and_then(|path| self.parse_path(path)) | ||
| 280 | .map(Expr::Path) | ||
| 281 | .unwrap_or(Expr::Missing); | ||
| 282 | self.alloc_expr(path, syntax_ptr) | ||
| 283 | } | ||
| 284 | ast::Expr::ContinueExpr(_e) => { | ||
| 285 | // FIXME: labels | ||
| 286 | self.alloc_expr(Expr::Continue, syntax_ptr) | ||
| 287 | } | ||
| 288 | ast::Expr::BreakExpr(e) => { | ||
| 289 | let expr = e.expr().map(|e| self.collect_expr(e)); | ||
| 290 | self.alloc_expr(Expr::Break { expr }, syntax_ptr) | ||
| 291 | } | ||
| 292 | ast::Expr::ParenExpr(e) => { | ||
| 293 | let inner = self.collect_expr_opt(e.expr()); | ||
| 294 | // make the paren expr point to the inner expression as well | ||
| 295 | self.source_map.expr_map.insert(Either::A(syntax_ptr), inner); | ||
| 296 | inner | ||
| 297 | } | ||
| 298 | ast::Expr::ReturnExpr(e) => { | ||
| 299 | let expr = e.expr().map(|e| self.collect_expr(e)); | ||
| 300 | self.alloc_expr(Expr::Return { expr }, syntax_ptr) | ||
| 301 | } | ||
| 302 | ast::Expr::RecordLit(e) => { | ||
| 303 | let path = e.path().and_then(|path| self.parse_path(path)); | ||
| 304 | let mut field_ptrs = Vec::new(); | ||
| 305 | let record_lit = if let Some(nfl) = e.record_field_list() { | ||
| 306 | let fields = nfl | ||
| 307 | .fields() | ||
| 308 | .inspect(|field| field_ptrs.push(AstPtr::new(field))) | ||
| 309 | .map(|field| RecordLitField { | ||
| 310 | name: field | ||
| 311 | .name_ref() | ||
| 312 | .map(|nr| nr.as_name()) | ||
| 313 | .unwrap_or_else(Name::missing), | ||
| 314 | expr: if let Some(e) = field.expr() { | ||
| 315 | self.collect_expr(e) | ||
| 316 | } else if let Some(nr) = field.name_ref() { | ||
| 317 | // field shorthand | ||
| 318 | self.alloc_expr_field_shorthand( | ||
| 319 | Expr::Path(Path::from_name_ref(&nr)), | ||
| 320 | AstPtr::new(&field), | ||
| 321 | ) | ||
| 322 | } else { | ||
| 323 | self.missing_expr() | ||
| 324 | }, | ||
| 325 | }) | ||
| 326 | .collect(); | ||
| 327 | let spread = nfl.spread().map(|s| self.collect_expr(s)); | ||
| 328 | Expr::RecordLit { path, fields, spread } | ||
| 329 | } else { | ||
| 330 | Expr::RecordLit { path, fields: Vec::new(), spread: None } | ||
| 331 | }; | ||
| 332 | |||
| 333 | let res = self.alloc_expr(record_lit, syntax_ptr); | ||
| 334 | for (i, ptr) in field_ptrs.into_iter().enumerate() { | ||
| 335 | self.source_map.field_map.insert((res, i), ptr); | ||
| 336 | } | ||
| 337 | res | ||
| 338 | } | ||
| 339 | ast::Expr::FieldExpr(e) => { | ||
| 340 | let expr = self.collect_expr_opt(e.expr()); | ||
| 341 | let name = match e.field_access() { | ||
| 342 | Some(kind) => kind.as_name(), | ||
| 343 | _ => Name::missing(), | ||
| 344 | }; | ||
| 345 | self.alloc_expr(Expr::Field { expr, name }, syntax_ptr) | ||
| 346 | } | ||
| 347 | ast::Expr::AwaitExpr(e) => { | ||
| 348 | let expr = self.collect_expr_opt(e.expr()); | ||
| 349 | self.alloc_expr(Expr::Await { expr }, syntax_ptr) | ||
| 350 | } | ||
| 351 | ast::Expr::TryExpr(e) => { | ||
| 352 | let expr = self.collect_expr_opt(e.expr()); | ||
| 353 | self.alloc_expr(Expr::Try { expr }, syntax_ptr) | ||
| 354 | } | ||
| 355 | ast::Expr::CastExpr(e) => { | ||
| 356 | let expr = self.collect_expr_opt(e.expr()); | ||
| 357 | let type_ref = TypeRef::from_ast_opt(e.type_ref()); | ||
| 358 | self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr) | ||
| 359 | } | ||
| 360 | ast::Expr::RefExpr(e) => { | ||
| 361 | let expr = self.collect_expr_opt(e.expr()); | ||
| 362 | let mutability = Mutability::from_mutable(e.is_mut()); | ||
| 363 | self.alloc_expr(Expr::Ref { expr, mutability }, syntax_ptr) | ||
| 364 | } | ||
| 365 | ast::Expr::PrefixExpr(e) => { | ||
| 366 | let expr = self.collect_expr_opt(e.expr()); | ||
| 367 | if let Some(op) = e.op_kind() { | ||
| 368 | self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr) | ||
| 369 | } else { | ||
| 370 | self.alloc_expr(Expr::Missing, syntax_ptr) | ||
| 371 | } | ||
| 372 | } | ||
| 373 | ast::Expr::LambdaExpr(e) => { | ||
| 374 | let mut args = Vec::new(); | ||
| 375 | let mut arg_types = Vec::new(); | ||
| 376 | if let Some(pl) = e.param_list() { | ||
| 377 | for param in pl.params() { | ||
| 378 | let pat = self.collect_pat_opt(param.pat()); | ||
| 379 | let type_ref = param.ascribed_type().map(TypeRef::from_ast); | ||
| 380 | args.push(pat); | ||
| 381 | arg_types.push(type_ref); | ||
| 382 | } | ||
| 383 | } | ||
| 384 | let body = self.collect_expr_opt(e.body()); | ||
| 385 | self.alloc_expr(Expr::Lambda { args, arg_types, body }, syntax_ptr) | ||
| 386 | } | ||
| 387 | ast::Expr::BinExpr(e) => { | ||
| 388 | let lhs = self.collect_expr_opt(e.lhs()); | ||
| 389 | let rhs = self.collect_expr_opt(e.rhs()); | ||
| 390 | let op = e.op_kind().map(BinaryOp::from); | ||
| 391 | self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr) | ||
| 392 | } | ||
| 393 | ast::Expr::TupleExpr(e) => { | ||
| 394 | let exprs = e.exprs().map(|expr| self.collect_expr(expr)).collect(); | ||
| 395 | self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr) | ||
| 396 | } | ||
| 397 | ast::Expr::BoxExpr(e) => { | ||
| 398 | let expr = self.collect_expr_opt(e.expr()); | ||
| 399 | self.alloc_expr(Expr::Box { expr }, syntax_ptr) | ||
| 400 | } | ||
| 401 | |||
| 402 | ast::Expr::ArrayExpr(e) => { | ||
| 403 | let kind = e.kind(); | ||
| 404 | |||
| 405 | match kind { | ||
| 406 | ArrayExprKind::ElementList(e) => { | ||
| 407 | let exprs = e.map(|expr| self.collect_expr(expr)).collect(); | ||
| 408 | self.alloc_expr(Expr::Array(Array::ElementList(exprs)), syntax_ptr) | ||
| 409 | } | ||
| 410 | ArrayExprKind::Repeat { initializer, repeat } => { | ||
| 411 | let initializer = self.collect_expr_opt(initializer); | ||
| 412 | let repeat = self.collect_expr_opt(repeat); | ||
| 413 | self.alloc_expr( | ||
| 414 | Expr::Array(Array::Repeat { initializer, repeat }), | ||
| 415 | syntax_ptr, | ||
| 416 | ) | ||
| 417 | } | ||
| 418 | } | ||
| 419 | } | ||
| 420 | |||
| 421 | ast::Expr::Literal(e) => { | ||
| 422 | let lit = match e.kind() { | ||
| 423 | LiteralKind::IntNumber { suffix } => { | ||
| 424 | let known_name = suffix.and_then(|it| BuiltinInt::from_suffix(&it)); | ||
| 425 | |||
| 426 | Literal::Int(Default::default(), known_name) | ||
| 427 | } | ||
| 428 | LiteralKind::FloatNumber { suffix } => { | ||
| 429 | let known_name = suffix.and_then(|it| BuiltinFloat::from_suffix(&it)); | ||
| 430 | |||
| 431 | Literal::Float(Default::default(), known_name) | ||
| 432 | } | ||
| 433 | LiteralKind::ByteString => Literal::ByteString(Default::default()), | ||
| 434 | LiteralKind::String => Literal::String(Default::default()), | ||
| 435 | LiteralKind::Byte => Literal::Int(Default::default(), Some(BuiltinInt::U8)), | ||
| 436 | LiteralKind::Bool => Literal::Bool(Default::default()), | ||
| 437 | LiteralKind::Char => Literal::Char(Default::default()), | ||
| 438 | }; | ||
| 439 | self.alloc_expr(Expr::Literal(lit), syntax_ptr) | ||
| 440 | } | ||
| 441 | ast::Expr::IndexExpr(e) => { | ||
| 442 | let base = self.collect_expr_opt(e.base()); | ||
| 443 | let index = self.collect_expr_opt(e.index()); | ||
| 444 | self.alloc_expr(Expr::Index { base, index }, syntax_ptr) | ||
| 445 | } | ||
| 446 | |||
| 447 | // FIXME implement HIR for these: | ||
| 448 | ast::Expr::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), | ||
| 449 | ast::Expr::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), | ||
| 450 | ast::Expr::MacroCall(e) => { | ||
| 451 | let ast_id = AstId::new( | ||
| 452 | self.current_file_id, | ||
| 453 | self.db.ast_id_map(self.current_file_id).ast_id(&e), | ||
| 454 | ); | ||
| 455 | |||
| 456 | if let Some(path) = e.path().and_then(|path| self.parse_path(path)) { | ||
| 457 | if let Some(def) = self.resolver.resolve_path_as_macro(self.db, &path) { | ||
| 458 | let call_id = self.db.intern_macro(MacroCallLoc { def, ast_id }); | ||
| 459 | let file_id = call_id.as_file(MacroFileKind::Expr); | ||
| 460 | if let Some(node) = self.db.parse_or_expand(file_id) { | ||
| 461 | if let Some(expr) = ast::Expr::cast(node) { | ||
| 462 | log::debug!("macro expansion {:#?}", expr.syntax()); | ||
| 463 | let old_file_id = | ||
| 464 | std::mem::replace(&mut self.current_file_id, file_id); | ||
| 465 | let id = self.collect_expr(expr); | ||
| 466 | self.current_file_id = old_file_id; | ||
| 467 | return id; | ||
| 468 | } | ||
| 469 | } | ||
| 470 | } | ||
| 471 | } | ||
| 472 | // FIXME: Instead of just dropping the error from expansion | ||
| 473 | // report it | ||
| 474 | self.alloc_expr(Expr::Missing, syntax_ptr) | ||
| 475 | } | ||
| 476 | } | ||
| 477 | } | ||
| 478 | |||
| 479 | fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId { | ||
| 480 | if let Some(expr) = expr { | ||
| 481 | self.collect_expr(expr) | ||
| 482 | } else { | ||
| 483 | self.missing_expr() | ||
| 484 | } | ||
| 485 | } | ||
| 486 | |||
| 487 | fn collect_block(&mut self, expr: ast::BlockExpr) -> ExprId { | ||
| 488 | let syntax_node_ptr = AstPtr::new(&expr.clone().into()); | ||
| 489 | let block = match expr.block() { | ||
| 490 | Some(block) => block, | ||
| 491 | None => return self.alloc_expr(Expr::Missing, syntax_node_ptr), | ||
| 492 | }; | ||
| 493 | let statements = block | ||
| 494 | .statements() | ||
| 495 | .map(|s| match s { | ||
| 496 | ast::Stmt::LetStmt(stmt) => { | ||
| 497 | let pat = self.collect_pat_opt(stmt.pat()); | ||
| 498 | let type_ref = stmt.ascribed_type().map(TypeRef::from_ast); | ||
| 499 | let initializer = stmt.initializer().map(|e| self.collect_expr(e)); | ||
| 500 | Statement::Let { pat, type_ref, initializer } | ||
| 501 | } | ||
| 502 | ast::Stmt::ExprStmt(stmt) => Statement::Expr(self.collect_expr_opt(stmt.expr())), | ||
| 503 | }) | ||
| 504 | .collect(); | ||
| 505 | let tail = block.expr().map(|e| self.collect_expr(e)); | ||
| 506 | self.alloc_expr(Expr::Block { statements, tail }, syntax_node_ptr) | ||
| 507 | } | ||
| 508 | |||
| 509 | fn collect_block_opt(&mut self, expr: Option<ast::BlockExpr>) -> ExprId { | ||
| 510 | if let Some(block) = expr { | ||
| 511 | self.collect_block(block) | ||
| 512 | } else { | ||
| 513 | self.missing_expr() | ||
| 514 | } | ||
| 515 | } | ||
| 516 | |||
| 517 | fn collect_pat(&mut self, pat: ast::Pat) -> PatId { | ||
| 518 | let pattern = match &pat { | ||
| 519 | ast::Pat::BindPat(bp) => { | ||
| 520 | let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); | ||
| 521 | let annotation = BindingAnnotation::new(bp.is_mutable(), bp.is_ref()); | ||
| 522 | let subpat = bp.pat().map(|subpat| self.collect_pat(subpat)); | ||
| 523 | Pat::Bind { name, mode: annotation, subpat } | ||
| 524 | } | ||
| 525 | ast::Pat::TupleStructPat(p) => { | ||
| 526 | let path = p.path().and_then(|path| self.parse_path(path)); | ||
| 527 | let args = p.args().map(|p| self.collect_pat(p)).collect(); | ||
| 528 | Pat::TupleStruct { path, args } | ||
| 529 | } | ||
| 530 | ast::Pat::RefPat(p) => { | ||
| 531 | let pat = self.collect_pat_opt(p.pat()); | ||
| 532 | let mutability = Mutability::from_mutable(p.is_mut()); | ||
| 533 | Pat::Ref { pat, mutability } | ||
| 534 | } | ||
| 535 | ast::Pat::PathPat(p) => { | ||
| 536 | let path = p.path().and_then(|path| self.parse_path(path)); | ||
| 537 | path.map(Pat::Path).unwrap_or(Pat::Missing) | ||
| 538 | } | ||
| 539 | ast::Pat::TuplePat(p) => { | ||
| 540 | let args = p.args().map(|p| self.collect_pat(p)).collect(); | ||
| 541 | Pat::Tuple(args) | ||
| 542 | } | ||
| 543 | ast::Pat::PlaceholderPat(_) => Pat::Wild, | ||
| 544 | ast::Pat::RecordPat(p) => { | ||
| 545 | let path = p.path().and_then(|path| self.parse_path(path)); | ||
| 546 | let record_field_pat_list = | ||
| 547 | p.record_field_pat_list().expect("every struct should have a field list"); | ||
| 548 | let mut fields: Vec<_> = record_field_pat_list | ||
| 549 | .bind_pats() | ||
| 550 | .filter_map(|bind_pat| { | ||
| 551 | let ast_pat = | ||
| 552 | ast::Pat::cast(bind_pat.syntax().clone()).expect("bind pat is a pat"); | ||
| 553 | let pat = self.collect_pat(ast_pat); | ||
| 554 | let name = bind_pat.name()?.as_name(); | ||
| 555 | Some(RecordFieldPat { name, pat }) | ||
| 556 | }) | ||
| 557 | .collect(); | ||
| 558 | let iter = record_field_pat_list.record_field_pats().filter_map(|f| { | ||
| 559 | let ast_pat = f.pat()?; | ||
| 560 | let pat = self.collect_pat(ast_pat); | ||
| 561 | let name = f.name()?.as_name(); | ||
| 562 | Some(RecordFieldPat { name, pat }) | ||
| 563 | }); | ||
| 564 | fields.extend(iter); | ||
| 565 | |||
| 566 | Pat::Record { path, args: fields } | ||
| 567 | } | ||
| 568 | |||
| 569 | // FIXME: implement | ||
| 570 | ast::Pat::DotDotPat(_) => Pat::Missing, | ||
| 571 | ast::Pat::BoxPat(_) => Pat::Missing, | ||
| 572 | ast::Pat::LiteralPat(_) => Pat::Missing, | ||
| 573 | ast::Pat::SlicePat(_) | ast::Pat::RangePat(_) => Pat::Missing, | ||
| 574 | }; | ||
| 575 | let ptr = AstPtr::new(&pat); | ||
| 576 | self.alloc_pat(pattern, Either::A(ptr)) | ||
| 577 | } | ||
| 578 | |||
| 579 | fn collect_pat_opt(&mut self, pat: Option<ast::Pat>) -> PatId { | ||
| 580 | if let Some(pat) = pat { | ||
| 581 | self.collect_pat(pat) | ||
| 582 | } else { | ||
| 583 | self.missing_pat() | ||
| 584 | } | ||
| 585 | } | ||
| 586 | |||
| 587 | fn parse_path(&mut self, path: ast::Path) -> Option<Path> { | ||
| 588 | let hygiene = Hygiene::new(self.db, self.current_file_id); | ||
| 589 | Path::from_src(path, &hygiene) | ||
| 590 | } | ||
| 591 | } | ||
| 6 | 592 | ||
| 7 | impl From<ast::BinOp> for BinaryOp { | 593 | impl From<ast::BinOp> for BinaryOp { |
| 8 | fn from(ast_op: ast::BinOp) -> Self { | 594 | fn from(ast_op: ast::BinOp) -> Self { |
