rust-analyzer/crates/ra_hir/src/expr.rs

173 lines
5.3 KiB
Rust
Raw Normal View History

//! FIXME: write short doc here
2019-09-03 05:56:36 +00:00
pub(crate) mod lower;
pub(crate) mod scope;
pub(crate) mod validation;
2019-01-05 15:32:07 +00:00
2019-09-03 05:56:36 +00:00
use std::{ops::Index, sync::Arc};
2019-01-05 15:32:07 +00:00
2019-11-12 12:09:25 +00:00
use ra_arena::{map::ArenaMap, Arena};
2019-09-03 05:56:36 +00:00
use ra_syntax::{ast, AstPtr};
use rustc_hash::FxHashMap;
2019-01-05 15:32:07 +00:00
2019-11-12 12:09:25 +00:00
use crate::{db::HirDatabase, DefWithBody, Either, HasSource, Resolver, Source};
2019-01-05 15:32:07 +00:00
2019-04-13 08:24:09 +00:00
pub use self::scope::ExprScopes;
2019-11-12 12:09:25 +00:00
pub use hir_def::expr::{
ArithOp, Array, BinaryOp, BindingAnnotation, CmpOp, Expr, ExprId, Literal, LogicOp, MatchArm,
Ordering, Pat, PatId, RecordFieldPat, RecordLitField, Statement, UnaryOp,
};
2019-09-03 05:56:36 +00:00
2019-01-05 15:32:07 +00:00
/// The body of an item (function, const etc.).
#[derive(Debug, Eq, PartialEq)]
pub struct Body {
exprs: Arena<ExprId, Expr>,
pats: Arena<PatId, Pat>,
2019-01-12 20:58:16 +00:00
/// The patterns for the function's parameters. While the parameter types are
2019-01-05 15:32:07 +00:00
/// part of the function signature, the patterns are not (they don't change
/// the external type of the function).
///
/// If this `Body` is for the body of a constant, this will just be
2019-01-05 15:32:07 +00:00
/// empty.
2019-01-12 20:58:16 +00:00
params: Vec<PatId>,
2019-01-05 15:32:07 +00:00
/// The `ExprId` of the actual body expression.
body_expr: ExprId,
}
type ExprPtr = Either<AstPtr<ast::Expr>, AstPtr<ast::RecordField>>;
type ExprSource = Source<ExprPtr>;
type PatPtr = Either<AstPtr<ast::Pat>, AstPtr<ast::SelfParam>>;
type PatSource = Source<PatPtr>;
2019-01-05 15:32:07 +00:00
/// An item body together with the mapping from syntax nodes to HIR expression
/// IDs. This is needed to go from e.g. a position in a file to the HIR
/// expression containing it; but for type inference etc., we want to operate on
/// a structure that is agnostic to the actual positions of expressions in the
/// file, so that we don't recompute types whenever some whitespace is typed.
///
/// One complication here is that, due to macro expansion, a single `Body` might
/// be spread across several files. So, for each ExprId and PatId, we record
/// both the HirFileId and the position inside the file. However, we only store
/// AST -> ExprId mapping for non-macro files, as it is not clear how to handle
/// this properly for macros.
2019-03-02 13:18:40 +00:00
#[derive(Default, Debug, Eq, PartialEq)]
2019-03-02 12:14:37 +00:00
pub struct BodySourceMap {
2019-09-02 18:23:19 +00:00
expr_map: FxHashMap<ExprPtr, ExprId>,
expr_map_back: ArenaMap<ExprId, ExprSource>,
2019-04-11 08:13:31 +00:00
pat_map: FxHashMap<PatPtr, PatId>,
pat_map_back: ArenaMap<PatId, PatSource>,
2019-08-23 12:55:21 +00:00
field_map: FxHashMap<(ExprId, usize), AstPtr<ast::RecordField>>,
2019-01-05 15:32:07 +00:00
}
impl Body {
2019-11-12 08:48:34 +00:00
pub(crate) fn body_with_source_map_query(
db: &impl HirDatabase,
def: DefWithBody,
) -> (Arc<Body>, Arc<BodySourceMap>) {
let mut params = None;
let (file_id, body) = match def {
DefWithBody::Function(f) => {
let src = f.source(db);
params = src.ast.param_list();
(src.file_id, src.ast.body().map(ast::Expr::from))
}
DefWithBody::Const(c) => {
let src = c.source(db);
(src.file_id, src.ast.body())
}
DefWithBody::Static(s) => {
let src = s.source(db);
(src.file_id, src.ast.body())
}
};
2019-11-12 13:46:27 +00:00
let (body, source_map) = lower::lower(db, def.resolver(db), file_id, params, body);
2019-11-12 08:48:34 +00:00
(Arc::new(body), Arc::new(source_map))
}
pub(crate) fn body_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<Body> {
db.body_with_source_map(def).0
}
2019-01-12 20:58:16 +00:00
pub fn params(&self) -> &[PatId] {
&self.params
}
pub fn body_expr(&self) -> ExprId {
self.body_expr
}
2019-01-19 20:23:26 +00:00
pub fn exprs(&self) -> impl Iterator<Item = (ExprId, &Expr)> {
self.exprs.iter()
}
pub fn pats(&self) -> impl Iterator<Item = (PatId, &Pat)> {
self.pats.iter()
}
}
2019-01-23 22:08:41 +00:00
// needs arbitrary_self_types to be a method... or maybe move to the def?
2019-04-13 08:02:23 +00:00
pub(crate) fn resolver_for_expr(
db: &impl HirDatabase,
2019-11-12 13:46:27 +00:00
owner: DefWithBody,
2019-04-13 08:02:23 +00:00
expr_id: ExprId,
) -> Resolver {
2019-11-12 13:46:27 +00:00
let scopes = db.expr_scopes(owner);
resolver_for_scope(db, owner, scopes.scope_for(expr_id))
2019-01-27 19:50:57 +00:00
}
2019-04-13 08:02:23 +00:00
pub(crate) fn resolver_for_scope(
2019-01-27 19:50:57 +00:00
db: &impl HirDatabase,
2019-11-12 13:46:27 +00:00
owner: DefWithBody,
2019-01-27 19:50:57 +00:00
scope_id: Option<scope::ScopeId>,
2019-01-27 16:23:49 +00:00
) -> Resolver {
2019-11-12 13:46:27 +00:00
let mut r = owner.resolver(db);
let scopes = db.expr_scopes(owner);
let scope_chain = scopes.scope_chain(scope_id).collect::<Vec<_>>();
2019-01-23 22:08:41 +00:00
for scope in scope_chain.into_iter().rev() {
r = r.push_expr_scope(Arc::clone(&scopes), scope);
}
r
}
2019-01-05 23:33:58 +00:00
impl Index<ExprId> for Body {
type Output = Expr;
fn index(&self, expr: ExprId) -> &Expr {
&self.exprs[expr]
}
}
impl Index<PatId> for Body {
type Output = Pat;
fn index(&self, pat: PatId) -> &Pat {
&self.pats[pat]
}
}
2019-03-02 12:14:37 +00:00
impl BodySourceMap {
pub(crate) fn expr_syntax(&self, expr: ExprId) -> Option<ExprSource> {
self.expr_map_back.get(expr).copied()
}
2019-04-10 08:15:55 +00:00
pub(crate) fn node_expr(&self, node: &ast::Expr) -> Option<ExprId> {
2019-09-02 18:23:19 +00:00
self.expr_map.get(&Either::A(AstPtr::new(node))).cloned()
}
pub(crate) fn pat_syntax(&self, pat: PatId) -> Option<PatSource> {
self.pat_map_back.get(pat).copied()
}
2019-04-10 08:15:55 +00:00
pub(crate) fn node_pat(&self, node: &ast::Pat) -> Option<PatId> {
2019-04-10 07:46:43 +00:00
self.pat_map.get(&Either::A(AstPtr::new(node))).cloned()
}
2019-03-21 19:13:11 +00:00
2019-08-23 12:55:21 +00:00
pub(crate) fn field_syntax(&self, expr: ExprId, field: usize) -> AstPtr<ast::RecordField> {
2019-07-04 17:26:44 +00:00
self.field_map[&(expr, field)]
2019-03-21 19:13:11 +00:00
}
}