Addeded resolver and db

This commit is contained in:
Lenard Pratt 2019-04-15 10:09:58 +01:00
parent 1705e5887d
commit afaeb18910
2 changed files with 92 additions and 60 deletions

View file

@ -12,6 +12,7 @@ use ra_syntax::{
use crate::{ use crate::{
Path, Name, HirDatabase, Resolver,DefWithBody, Either, Path, Name, HirDatabase, Resolver,DefWithBody, Either,
name::AsName, name::AsName,
ids::MacroDefId,
type_ref::{Mutability, TypeRef}, type_ref::{Mutability, TypeRef},
}; };
use crate::{path::GenericArgs, ty::primitive::{IntTy, UncertainIntTy, FloatTy, UncertainFloatTy}}; use crate::{path::GenericArgs, ty::primitive::{IntTy, UncertainIntTy, FloatTy, UncertainFloatTy}};
@ -485,17 +486,20 @@ pub(crate) struct ExprCollector {
source_map: BodySourceMap, source_map: BodySourceMap,
params: Vec<PatId>, params: Vec<PatId>,
body_expr: Option<ExprId>, body_expr: Option<ExprId>,
resolver: Resolver,
} }
impl ExprCollector { impl ExprCollector{
fn new(owner: DefWithBody) -> Self { fn new(owner: DefWithBody,resolver:Resolver) -> Self {
ExprCollector { ExprCollector {
owner, owner,
resolver,
exprs: Arena::default(), exprs: Arena::default(),
pats: Arena::default(), pats: Arena::default(),
source_map: BodySourceMap::default(), source_map: BodySourceMap::default(),
params: Vec::new(), params: Vec::new(),
body_expr: None, body_expr: None,
} }
} }
@ -518,7 +522,7 @@ impl ExprCollector {
self.exprs.alloc(block) self.exprs.alloc(block)
} }
fn collect_expr(&mut self, expr: &ast::Expr) -> ExprId { fn collect_expr(&mut self, expr: &ast::Expr,db:&impl HirDatabase) -> ExprId {
let syntax_ptr = SyntaxNodePtr::new(expr.syntax()); let syntax_ptr = SyntaxNodePtr::new(expr.syntax());
match expr.kind() { match expr.kind() {
ast::ExprKind::IfExpr(e) => { ast::ExprKind::IfExpr(e) => {
@ -526,15 +530,15 @@ impl ExprCollector {
// if let -- desugar to match // if let -- desugar to match
let pat = self.collect_pat(pat); let pat = self.collect_pat(pat);
let match_expr = let match_expr =
self.collect_expr_opt(e.condition().expect("checked above").expr()); self.collect_expr_opt(e.condition().expect("checked above").expr(),db);
let then_branch = self.collect_block_opt(e.then_branch()); let then_branch = self.collect_block_opt(e.then_branch(),db);
let else_branch = e let else_branch = e
.else_branch() .else_branch()
.map(|b| match b { .map(|b| match b {
ast::ElseBranch::Block(it) => self.collect_block(it), ast::ElseBranch::Block(it) => self.collect_block(it,db),
ast::ElseBranch::IfExpr(elif) => { ast::ElseBranch::IfExpr(elif) => {
let expr: &ast::Expr = ast::Expr::cast(elif.syntax()).unwrap(); let expr: &ast::Expr = ast::Expr::cast(elif.syntax()).unwrap();
self.collect_expr(expr) self.collect_expr(expr,db)
} }
}) })
.unwrap_or_else(|| self.empty_block()); .unwrap_or_else(|| self.empty_block());
@ -545,27 +549,27 @@ impl ExprCollector {
]; ];
self.alloc_expr(Expr::Match { expr: match_expr, arms }, syntax_ptr) self.alloc_expr(Expr::Match { expr: match_expr, arms }, syntax_ptr)
} else { } else {
let condition = self.collect_expr_opt(e.condition().and_then(|c| c.expr())); let condition = self.collect_expr_opt(e.condition().and_then(|c| c.expr()),db);
let then_branch = self.collect_block_opt(e.then_branch()); let then_branch = self.collect_block_opt(e.then_branch(),db);
let else_branch = e.else_branch().map(|b| match b { let else_branch = e.else_branch().map(|b| match b {
ast::ElseBranch::Block(it) => self.collect_block(it), ast::ElseBranch::Block(it) => self.collect_block(it,db),
ast::ElseBranch::IfExpr(elif) => { ast::ElseBranch::IfExpr(elif) => {
let expr: &ast::Expr = ast::Expr::cast(elif.syntax()).unwrap(); let expr: &ast::Expr = ast::Expr::cast(elif.syntax()).unwrap();
self.collect_expr(expr) self.collect_expr(expr,db)
} }
}); });
self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr) self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr)
} }
} }
ast::ExprKind::BlockExpr(e) => self.collect_block_opt(e.block()), ast::ExprKind::BlockExpr(e) => self.collect_block_opt(e.block(),db),
ast::ExprKind::LoopExpr(e) => { ast::ExprKind::LoopExpr(e) => {
let body = self.collect_block_opt(e.loop_body()); let body = self.collect_block_opt(e.loop_body(),db);
self.alloc_expr(Expr::Loop { body }, syntax_ptr) self.alloc_expr(Expr::Loop { body }, syntax_ptr)
} }
ast::ExprKind::WhileExpr(e) => { ast::ExprKind::WhileExpr(e) => {
let condition = if let Some(condition) = e.condition() { let condition = if let Some(condition) = e.condition() {
if condition.pat().is_none() { if condition.pat().is_none() {
self.collect_expr_opt(condition.expr()) self.collect_expr_opt(condition.expr(),db)
} else { } else {
// FIXME handle while let // FIXME handle while let
return self.alloc_expr(Expr::Missing, syntax_ptr); return self.alloc_expr(Expr::Missing, syntax_ptr);
@ -573,28 +577,28 @@ impl ExprCollector {
} else { } else {
self.exprs.alloc(Expr::Missing) self.exprs.alloc(Expr::Missing)
}; };
let body = self.collect_block_opt(e.loop_body()); let body = self.collect_block_opt(e.loop_body(),db);
self.alloc_expr(Expr::While { condition, body }, syntax_ptr) self.alloc_expr(Expr::While { condition, body }, syntax_ptr)
} }
ast::ExprKind::ForExpr(e) => { ast::ExprKind::ForExpr(e) => {
let iterable = self.collect_expr_opt(e.iterable()); let iterable = self.collect_expr_opt(e.iterable(),db);
let pat = self.collect_pat_opt(e.pat()); let pat = self.collect_pat_opt(e.pat());
let body = self.collect_block_opt(e.loop_body()); let body = self.collect_block_opt(e.loop_body(),db);
self.alloc_expr(Expr::For { iterable, pat, body }, syntax_ptr) self.alloc_expr(Expr::For { iterable, pat, body }, syntax_ptr)
} }
ast::ExprKind::CallExpr(e) => { ast::ExprKind::CallExpr(e) => {
let callee = self.collect_expr_opt(e.expr()); let callee = self.collect_expr_opt(e.expr(),db);
let args = if let Some(arg_list) = e.arg_list() { let args = if let Some(arg_list) = e.arg_list() {
arg_list.args().map(|e| self.collect_expr(e)).collect() arg_list.args().map(|e| self.collect_expr(e,db)).collect()
} else { } else {
Vec::new() Vec::new()
}; };
self.alloc_expr(Expr::Call { callee, args }, syntax_ptr) self.alloc_expr(Expr::Call { callee, args }, syntax_ptr)
} }
ast::ExprKind::MethodCallExpr(e) => { ast::ExprKind::MethodCallExpr(e) => {
let receiver = self.collect_expr_opt(e.expr()); let receiver = self.collect_expr_opt(e.expr(),db);
let args = if let Some(arg_list) = e.arg_list() { let args = if let Some(arg_list) = e.arg_list() {
arg_list.args().map(|e| self.collect_expr(e)).collect() arg_list.args().map(|e| self.collect_expr(e,db)).collect()
} else { } else {
Vec::new() Vec::new()
}; };
@ -606,17 +610,17 @@ impl ExprCollector {
) )
} }
ast::ExprKind::MatchExpr(e) => { ast::ExprKind::MatchExpr(e) => {
let expr = self.collect_expr_opt(e.expr()); let expr = self.collect_expr_opt(e.expr(),db);
let arms = if let Some(match_arm_list) = e.match_arm_list() { let arms = if let Some(match_arm_list) = e.match_arm_list() {
match_arm_list match_arm_list
.arms() .arms()
.map(|arm| MatchArm { .map(|arm| MatchArm {
pats: arm.pats().map(|p| self.collect_pat(p)).collect(), pats: arm.pats().map(|p| self.collect_pat(p)).collect(),
expr: self.collect_expr_opt(arm.expr()), expr: self.collect_expr_opt(arm.expr(),db),
guard: arm guard: arm
.guard() .guard()
.and_then(|guard| guard.expr()) .and_then(|guard| guard.expr())
.map(|e| self.collect_expr(e)), .map(|e| self.collect_expr(e,db)),
}) })
.collect() .collect()
} else { } else {
@ -634,17 +638,17 @@ impl ExprCollector {
self.alloc_expr(Expr::Continue, syntax_ptr) self.alloc_expr(Expr::Continue, syntax_ptr)
} }
ast::ExprKind::BreakExpr(e) => { ast::ExprKind::BreakExpr(e) => {
let expr = e.expr().map(|e| self.collect_expr(e)); let expr = e.expr().map(|e| self.collect_expr(e,db));
self.alloc_expr(Expr::Break { expr }, syntax_ptr) self.alloc_expr(Expr::Break { expr }, syntax_ptr)
} }
ast::ExprKind::ParenExpr(e) => { ast::ExprKind::ParenExpr(e) => {
let inner = self.collect_expr_opt(e.expr()); let inner = self.collect_expr_opt(e.expr(),db);
// make the paren expr point to the inner expression as well // make the paren expr point to the inner expression as well
self.source_map.expr_map.insert(syntax_ptr, inner); self.source_map.expr_map.insert(syntax_ptr, inner);
inner inner
} }
ast::ExprKind::ReturnExpr(e) => { ast::ExprKind::ReturnExpr(e) => {
let expr = e.expr().map(|e| self.collect_expr(e)); let expr = e.expr().map(|e| self.collect_expr(e,db));
self.alloc_expr(Expr::Return { expr }, syntax_ptr) self.alloc_expr(Expr::Return { expr }, syntax_ptr)
} }
ast::ExprKind::StructLit(e) => { ast::ExprKind::StructLit(e) => {
@ -659,7 +663,7 @@ impl ExprCollector {
.map(|nr| nr.as_name()) .map(|nr| nr.as_name())
.unwrap_or_else(Name::missing), .unwrap_or_else(Name::missing),
expr: if let Some(e) = field.expr() { expr: if let Some(e) = field.expr() {
self.collect_expr(e) self.collect_expr(e,db)
} else if let Some(nr) = field.name_ref() { } else if let Some(nr) = field.name_ref() {
// field shorthand // field shorthand
let id = self.exprs.alloc(Expr::Path(Path::from_name_ref(nr))); let id = self.exprs.alloc(Expr::Path(Path::from_name_ref(nr)));
@ -678,7 +682,7 @@ impl ExprCollector {
} else { } else {
Vec::new() Vec::new()
}; };
let spread = e.spread().map(|s| self.collect_expr(s)); let spread = e.spread().map(|s| self.collect_expr(s,db));
let res = self.alloc_expr(Expr::StructLit { path, fields, spread }, syntax_ptr); let res = self.alloc_expr(Expr::StructLit { path, fields, spread }, syntax_ptr);
for (i, ptr) in field_ptrs.into_iter().enumerate() { for (i, ptr) in field_ptrs.into_iter().enumerate() {
self.source_map.field_map.insert((res, i), ptr); self.source_map.field_map.insert((res, i), ptr);
@ -686,7 +690,7 @@ impl ExprCollector {
res res
} }
ast::ExprKind::FieldExpr(e) => { ast::ExprKind::FieldExpr(e) => {
let expr = self.collect_expr_opt(e.expr()); let expr = self.collect_expr_opt(e.expr(),db);
let name = match e.field_access() { let name = match e.field_access() {
Some(kind) => kind.as_name(), Some(kind) => kind.as_name(),
_ => Name::missing(), _ => Name::missing(),
@ -694,21 +698,21 @@ impl ExprCollector {
self.alloc_expr(Expr::Field { expr, name }, syntax_ptr) self.alloc_expr(Expr::Field { expr, name }, syntax_ptr)
} }
ast::ExprKind::TryExpr(e) => { ast::ExprKind::TryExpr(e) => {
let expr = self.collect_expr_opt(e.expr()); let expr = self.collect_expr_opt(e.expr(),db);
self.alloc_expr(Expr::Try { expr }, syntax_ptr) self.alloc_expr(Expr::Try { expr }, syntax_ptr)
} }
ast::ExprKind::CastExpr(e) => { ast::ExprKind::CastExpr(e) => {
let expr = self.collect_expr_opt(e.expr()); let expr = self.collect_expr_opt(e.expr(),db);
let type_ref = TypeRef::from_ast_opt(e.type_ref()); let type_ref = TypeRef::from_ast_opt(e.type_ref());
self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr) self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr)
} }
ast::ExprKind::RefExpr(e) => { ast::ExprKind::RefExpr(e) => {
let expr = self.collect_expr_opt(e.expr()); let expr = self.collect_expr_opt(e.expr(),db);
let mutability = Mutability::from_mutable(e.is_mut()); let mutability = Mutability::from_mutable(e.is_mut());
self.alloc_expr(Expr::Ref { expr, mutability }, syntax_ptr) self.alloc_expr(Expr::Ref { expr, mutability }, syntax_ptr)
} }
ast::ExprKind::PrefixExpr(e) => { ast::ExprKind::PrefixExpr(e) => {
let expr = self.collect_expr_opt(e.expr()); let expr = self.collect_expr_opt(e.expr(),db);
if let Some(op) = e.op_kind() { if let Some(op) = e.op_kind() {
self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr) self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr)
} else { } else {
@ -726,17 +730,17 @@ impl ExprCollector {
arg_types.push(type_ref); arg_types.push(type_ref);
} }
} }
let body = self.collect_expr_opt(e.body()); let body = self.collect_expr_opt(e.body(),db);
self.alloc_expr(Expr::Lambda { args, arg_types, body }, syntax_ptr) self.alloc_expr(Expr::Lambda { args, arg_types, body }, syntax_ptr)
} }
ast::ExprKind::BinExpr(e) => { ast::ExprKind::BinExpr(e) => {
let lhs = self.collect_expr_opt(e.lhs()); let lhs = self.collect_expr_opt(e.lhs(),db);
let rhs = self.collect_expr_opt(e.rhs()); let rhs = self.collect_expr_opt(e.rhs(),db);
let op = e.op_kind(); let op = e.op_kind();
self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr) self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr)
} }
ast::ExprKind::TupleExpr(e) => { ast::ExprKind::TupleExpr(e) => {
let exprs = e.exprs().map(|expr| self.collect_expr(expr)).collect(); let exprs = e.exprs().map(|expr| self.collect_expr(expr,db)).collect();
self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr) self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr)
} }
@ -745,12 +749,12 @@ impl ExprCollector {
match kind { match kind {
ArrayExprKind::ElementList(e) => { ArrayExprKind::ElementList(e) => {
let exprs = e.map(|expr| self.collect_expr(expr)).collect(); let exprs = e.map(|expr| self.collect_expr(expr,db)).collect();
self.alloc_expr(Expr::Array(Array::ElementList(exprs)), syntax_ptr) self.alloc_expr(Expr::Array(Array::ElementList(exprs)), syntax_ptr)
} }
ArrayExprKind::Repeat { initializer, repeat } => { ArrayExprKind::Repeat { initializer, repeat } => {
let initializer = self.collect_expr_opt(initializer); let initializer = self.collect_expr_opt(initializer,db);
let repeat = self.collect_expr_opt(repeat); let repeat = self.collect_expr_opt(repeat,db);
self.alloc_expr( self.alloc_expr(
Expr::Array(Array::Repeat { initializer, repeat }), Expr::Array(Array::Repeat { initializer, repeat }),
syntax_ptr, syntax_ptr,
@ -794,40 +798,53 @@ impl ExprCollector {
ast::ExprKind::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), ast::ExprKind::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
ast::ExprKind::IndexExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), ast::ExprKind::IndexExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
ast::ExprKind::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), ast::ExprKind::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
ast::ExprKind::MacroCall(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), ast::ExprKind::MacroCall(e) => {
let name = e.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
let res = self.resolver.resolve_name(db,&name);
// match res {
// }
// let resolver = Resolver
self.alloc_expr(Expr::Missing, syntax_ptr)
},
} }
} }
fn collect_expr_opt(&mut self, expr: Option<&ast::Expr>) -> ExprId { fn collect_expr_opt(&mut self, expr: Option<&ast::Expr>,db:&impl HirDatabase) -> ExprId {
if let Some(expr) = expr { if let Some(expr) = expr {
self.collect_expr(expr) self.collect_expr(expr,db)
} else { } else {
self.exprs.alloc(Expr::Missing) self.exprs.alloc(Expr::Missing)
} }
} }
fn collect_block(&mut self, block: &ast::Block) -> ExprId { fn collect_block(&mut self, block: &ast::Block,db:&impl HirDatabase) -> ExprId {
let statements = block let statements = block
.statements() .statements()
.map(|s| match s.kind() { .map(|s| match s.kind() {
ast::StmtKind::LetStmt(stmt) => { ast::StmtKind::LetStmt(stmt) => {
let pat = self.collect_pat_opt(stmt.pat()); let pat = self.collect_pat_opt(stmt.pat());
let type_ref = stmt.ascribed_type().map(TypeRef::from_ast); let type_ref = stmt.ascribed_type().map(TypeRef::from_ast);
let initializer = stmt.initializer().map(|e| self.collect_expr(e)); let initializer = stmt.initializer().map(|e| self.collect_expr(e,db));
Statement::Let { pat, type_ref, initializer } Statement::Let { pat, type_ref, initializer }
} }
ast::StmtKind::ExprStmt(stmt) => { ast::StmtKind::ExprStmt(stmt) => {
Statement::Expr(self.collect_expr_opt(stmt.expr())) Statement::Expr(self.collect_expr_opt(stmt.expr(),db))
} }
}) })
.collect(); .collect();
let tail = block.expr().map(|e| self.collect_expr(e)); let tail = block.expr().map(|e| self.collect_expr(e,db));
self.alloc_expr(Expr::Block { statements, tail }, SyntaxNodePtr::new(block.syntax())) self.alloc_expr(Expr::Block { statements, tail }, SyntaxNodePtr::new(block.syntax()))
} }
fn collect_block_opt(&mut self, block: Option<&ast::Block>) -> ExprId { fn collect_block_opt(&mut self, block: Option<&ast::Block>,db:&impl HirDatabase) -> ExprId {
if let Some(block) = block { if let Some(block) = block {
self.collect_block(block) self.collect_block(block,db)
} else { } else {
self.exprs.alloc(Expr::Missing) self.exprs.alloc(Expr::Missing)
} }
@ -900,17 +917,17 @@ impl ExprCollector {
} }
} }
fn collect_const_body(&mut self, node: &ast::ConstDef) { fn collect_const_body(&mut self, node: &ast::ConstDef,db:&impl HirDatabase) {
let body = self.collect_expr_opt(node.body()); let body = self.collect_expr_opt(node.body(),db);
self.body_expr = Some(body); self.body_expr = Some(body);
} }
fn collect_static_body(&mut self, node: &ast::StaticDef) { fn collect_static_body(&mut self, node: &ast::StaticDef,db:&impl HirDatabase) {
let body = self.collect_expr_opt(node.body()); let body = self.collect_expr_opt(node.body(),db);
self.body_expr = Some(body); self.body_expr = Some(body);
} }
fn collect_fn_body(&mut self, node: &ast::FnDef) { fn collect_fn_body(&mut self, node: &ast::FnDef,db:&impl HirDatabase) {
if let Some(param_list) = node.param_list() { if let Some(param_list) = node.param_list() {
if let Some(self_param) = param_list.self_param() { if let Some(self_param) = param_list.self_param() {
let ptr = AstPtr::new(self_param); let ptr = AstPtr::new(self_param);
@ -936,7 +953,7 @@ impl ExprCollector {
} }
}; };
let body = self.collect_block_opt(node.body()); let body = self.collect_block_opt(node.body(),db);
self.body_expr = Some(body); self.body_expr = Some(body);
} }
@ -956,12 +973,14 @@ pub(crate) fn body_with_source_map_query(
db: &impl HirDatabase, db: &impl HirDatabase,
def: DefWithBody, def: DefWithBody,
) -> (Arc<Body>, Arc<BodySourceMap>) { ) -> (Arc<Body>, Arc<BodySourceMap>) {
let mut collector = ExprCollector::new(def);
let mut resolver = def.resolver(db);
let mut collector = ExprCollector::new(def,resolver);
match def { match def {
DefWithBody::Const(ref c) => collector.collect_const_body(&c.source(db).1), DefWithBody::Const(ref c) => collector.collect_const_body(&c.source(db).1,db),
DefWithBody::Function(ref f) => collector.collect_fn_body(&f.source(db).1), DefWithBody::Function(ref f) => collector.collect_fn_body(&f.source(db).1,db),
DefWithBody::Static(ref s) => collector.collect_static_body(&s.source(db).1), DefWithBody::Static(ref s) => collector.collect_static_body(&s.source(db).1,db),
} }
let (body, source_map) = collector.finish(); let (body, source_map) = collector.finish();

View file

@ -0,0 +1,13 @@
macro_rules! vec {
($($item:expr),*) =>
{
{
let mut v = Vec::new();
$(
v.push($item);
)*
v
}
};
}