2019-09-30 11:58:53 +03:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
|
2019-09-03 08:56:36 +03:00
|
|
|
pub(crate) mod scope;
|
|
|
|
|
pub(crate) mod validation;
|
2019-01-05 16:32:07 +01:00
|
|
|
|
2019-11-12 18:46:57 +03:00
|
|
|
use std::sync::Arc;
|
2019-01-05 16:32:07 +01:00
|
|
|
|
2019-09-03 08:56:36 +03:00
|
|
|
use ra_syntax::{ast, AstPtr};
|
2019-01-05 16:32:07 +01:00
|
|
|
|
2019-11-12 18:46:57 +03:00
|
|
|
use crate::{db::HirDatabase, DefWithBody, HasSource, Resolver};
|
2019-01-05 16:32:07 +01:00
|
|
|
|
2019-04-13 11:24:09 +03:00
|
|
|
pub use self::scope::ExprScopes;
|
2019-01-19 19:47:56 +01:00
|
|
|
|
2019-11-12 18:46:57 +03:00
|
|
|
pub use hir_def::{
|
|
|
|
|
body::{Body, BodySourceMap, ExprPtr, ExprSource, PatPtr, PatSource},
|
|
|
|
|
expr::{
|
|
|
|
|
ArithOp, Array, BinaryOp, BindingAnnotation, CmpOp, Expr, ExprId, Literal, LogicOp,
|
|
|
|
|
MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, Statement, UnaryOp,
|
|
|
|
|
},
|
2019-11-12 15:09:25 +03:00
|
|
|
};
|
2019-09-03 08:56:36 +03:00
|
|
|
|
2019-11-12 18:46:57 +03: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-14 09:38:25 +03:00
|
|
|
let expander = hir_def::body::Expander::new(db, file_id, def.module(db).id);
|
|
|
|
|
let (body, source_map) = Body::new(db, expander, params, body);
|
2019-11-12 18:46:57 +03:00
|
|
|
(Arc::new(body), Arc::new(source_map))
|
2019-01-05 16:32:07 +01:00
|
|
|
}
|
|
|
|
|
|
2019-11-12 18:46:57 +03:00
|
|
|
pub(crate) fn body_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<Body> {
|
|
|
|
|
db.body_with_source_map(def).0
|
2019-01-05 22:37:59 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-23 23:08:41 +01:00
|
|
|
// needs arbitrary_self_types to be a method... or maybe move to the def?
|
2019-04-13 11:02:23 +03:00
|
|
|
pub(crate) fn resolver_for_expr(
|
|
|
|
|
db: &impl HirDatabase,
|
2019-11-12 16:46:27 +03:00
|
|
|
owner: DefWithBody,
|
2019-04-13 11:02:23 +03:00
|
|
|
expr_id: ExprId,
|
|
|
|
|
) -> Resolver {
|
2019-11-12 16:46:27 +03:00
|
|
|
let scopes = db.expr_scopes(owner);
|
|
|
|
|
resolver_for_scope(db, owner, scopes.scope_for(expr_id))
|
2019-01-27 20:50:57 +01:00
|
|
|
}
|
|
|
|
|
|
2019-04-13 11:02:23 +03:00
|
|
|
pub(crate) fn resolver_for_scope(
|
2019-01-27 20:50:57 +01:00
|
|
|
db: &impl HirDatabase,
|
2019-11-12 16:46:27 +03:00
|
|
|
owner: DefWithBody,
|
2019-01-27 20:50:57 +01:00
|
|
|
scope_id: Option<scope::ScopeId>,
|
2019-01-27 17:23:49 +01:00
|
|
|
) -> Resolver {
|
2019-11-12 16:46:27 +03:00
|
|
|
let mut r = owner.resolver(db);
|
|
|
|
|
let scopes = db.expr_scopes(owner);
|
2019-04-13 10:49:01 +03:00
|
|
|
let scope_chain = scopes.scope_chain(scope_id).collect::<Vec<_>>();
|
2019-01-23 23:08:41 +01:00
|
|
|
for scope in scope_chain.into_iter().rev() {
|
|
|
|
|
r = r.push_expr_scope(Arc::clone(&scopes), scope);
|
|
|
|
|
}
|
|
|
|
|
r
|
|
|
|
|
}
|