mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 14:21:44 +00:00
Added macro resolution and expansion
This commit is contained in:
parent
afaeb18910
commit
ce211434a6
8 changed files with 134 additions and 103 deletions
|
@ -12,7 +12,7 @@ use ra_syntax::{
|
|||
use crate::{
|
||||
Path, Name, HirDatabase, Resolver,DefWithBody, Either,
|
||||
name::AsName,
|
||||
ids::MacroDefId,
|
||||
ids::{MacroCallLoc,HirFileId},
|
||||
type_ref::{Mutability, TypeRef},
|
||||
};
|
||||
use crate::{path::GenericArgs, ty::primitive::{IntTy, UncertainIntTy, FloatTy, UncertainFloatTy}};
|
||||
|
@ -479,7 +479,8 @@ impl Pat {
|
|||
|
||||
// Queries
|
||||
|
||||
pub(crate) struct ExprCollector {
|
||||
pub(crate) struct ExprCollector<DB> {
|
||||
db: DB,
|
||||
owner: DefWithBody,
|
||||
exprs: Arena<ExprId, Expr>,
|
||||
pats: Arena<PatId, Pat>,
|
||||
|
@ -489,20 +490,10 @@ pub(crate) struct ExprCollector {
|
|||
resolver: Resolver,
|
||||
}
|
||||
|
||||
impl ExprCollector{
|
||||
fn new(owner: DefWithBody,resolver:Resolver) -> Self {
|
||||
ExprCollector {
|
||||
owner,
|
||||
resolver,
|
||||
exprs: Arena::default(),
|
||||
pats: Arena::default(),
|
||||
source_map: BodySourceMap::default(),
|
||||
params: Vec::new(),
|
||||
body_expr: None,
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, DB> ExprCollector<&'a DB>
|
||||
where
|
||||
DB: HirDatabase,
|
||||
{
|
||||
fn alloc_expr(&mut self, expr: Expr, syntax_ptr: SyntaxNodePtr) -> ExprId {
|
||||
let id = self.exprs.alloc(expr);
|
||||
self.source_map.expr_map.insert(syntax_ptr, id);
|
||||
|
@ -522,7 +513,7 @@ impl ExprCollector{
|
|||
self.exprs.alloc(block)
|
||||
}
|
||||
|
||||
fn collect_expr(&mut self, expr: &ast::Expr,db:&impl HirDatabase) -> ExprId {
|
||||
fn collect_expr(&mut self, expr: &ast::Expr) -> ExprId {
|
||||
let syntax_ptr = SyntaxNodePtr::new(expr.syntax());
|
||||
match expr.kind() {
|
||||
ast::ExprKind::IfExpr(e) => {
|
||||
|
@ -530,15 +521,15 @@ impl ExprCollector{
|
|||
// if let -- desugar to match
|
||||
let pat = self.collect_pat(pat);
|
||||
let match_expr =
|
||||
self.collect_expr_opt(e.condition().expect("checked above").expr(),db);
|
||||
let then_branch = self.collect_block_opt(e.then_branch(),db);
|
||||
self.collect_expr_opt(e.condition().expect("checked above").expr());
|
||||
let then_branch = self.collect_block_opt(e.then_branch());
|
||||
let else_branch = e
|
||||
.else_branch()
|
||||
.map(|b| match b {
|
||||
ast::ElseBranch::Block(it) => self.collect_block(it,db),
|
||||
ast::ElseBranch::Block(it) => self.collect_block(it),
|
||||
ast::ElseBranch::IfExpr(elif) => {
|
||||
let expr: &ast::Expr = ast::Expr::cast(elif.syntax()).unwrap();
|
||||
self.collect_expr(expr,db)
|
||||
self.collect_expr(expr)
|
||||
}
|
||||
})
|
||||
.unwrap_or_else(|| self.empty_block());
|
||||
|
@ -549,27 +540,27 @@ impl ExprCollector{
|
|||
];
|
||||
self.alloc_expr(Expr::Match { expr: match_expr, arms }, syntax_ptr)
|
||||
} else {
|
||||
let condition = self.collect_expr_opt(e.condition().and_then(|c| c.expr()),db);
|
||||
let then_branch = self.collect_block_opt(e.then_branch(),db);
|
||||
let condition = self.collect_expr_opt(e.condition().and_then(|c| c.expr()));
|
||||
let then_branch = self.collect_block_opt(e.then_branch());
|
||||
let else_branch = e.else_branch().map(|b| match b {
|
||||
ast::ElseBranch::Block(it) => self.collect_block(it,db),
|
||||
ast::ElseBranch::Block(it) => self.collect_block(it),
|
||||
ast::ElseBranch::IfExpr(elif) => {
|
||||
let expr: &ast::Expr = ast::Expr::cast(elif.syntax()).unwrap();
|
||||
self.collect_expr(expr,db)
|
||||
self.collect_expr(expr)
|
||||
}
|
||||
});
|
||||
self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr)
|
||||
}
|
||||
}
|
||||
ast::ExprKind::BlockExpr(e) => self.collect_block_opt(e.block(),db),
|
||||
ast::ExprKind::BlockExpr(e) => self.collect_block_opt(e.block()),
|
||||
ast::ExprKind::LoopExpr(e) => {
|
||||
let body = self.collect_block_opt(e.loop_body(),db);
|
||||
let body = self.collect_block_opt(e.loop_body());
|
||||
self.alloc_expr(Expr::Loop { body }, syntax_ptr)
|
||||
}
|
||||
ast::ExprKind::WhileExpr(e) => {
|
||||
let condition = if let Some(condition) = e.condition() {
|
||||
if condition.pat().is_none() {
|
||||
self.collect_expr_opt(condition.expr(),db)
|
||||
self.collect_expr_opt(condition.expr())
|
||||
} else {
|
||||
// FIXME handle while let
|
||||
return self.alloc_expr(Expr::Missing, syntax_ptr);
|
||||
|
@ -577,28 +568,28 @@ impl ExprCollector{
|
|||
} else {
|
||||
self.exprs.alloc(Expr::Missing)
|
||||
};
|
||||
let body = self.collect_block_opt(e.loop_body(),db);
|
||||
let body = self.collect_block_opt(e.loop_body());
|
||||
self.alloc_expr(Expr::While { condition, body }, syntax_ptr)
|
||||
}
|
||||
ast::ExprKind::ForExpr(e) => {
|
||||
let iterable = self.collect_expr_opt(e.iterable(),db);
|
||||
let iterable = self.collect_expr_opt(e.iterable());
|
||||
let pat = self.collect_pat_opt(e.pat());
|
||||
let body = self.collect_block_opt(e.loop_body(),db);
|
||||
let body = self.collect_block_opt(e.loop_body());
|
||||
self.alloc_expr(Expr::For { iterable, pat, body }, syntax_ptr)
|
||||
}
|
||||
ast::ExprKind::CallExpr(e) => {
|
||||
let callee = self.collect_expr_opt(e.expr(),db);
|
||||
let callee = self.collect_expr_opt(e.expr());
|
||||
let args = if let Some(arg_list) = e.arg_list() {
|
||||
arg_list.args().map(|e| self.collect_expr(e,db)).collect()
|
||||
arg_list.args().map(|e| self.collect_expr(e)).collect()
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
self.alloc_expr(Expr::Call { callee, args }, syntax_ptr)
|
||||
}
|
||||
ast::ExprKind::MethodCallExpr(e) => {
|
||||
let receiver = self.collect_expr_opt(e.expr(),db);
|
||||
let receiver = self.collect_expr_opt(e.expr());
|
||||
let args = if let Some(arg_list) = e.arg_list() {
|
||||
arg_list.args().map(|e| self.collect_expr(e,db)).collect()
|
||||
arg_list.args().map(|e| self.collect_expr(e)).collect()
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
|
@ -610,17 +601,17 @@ impl ExprCollector{
|
|||
)
|
||||
}
|
||||
ast::ExprKind::MatchExpr(e) => {
|
||||
let expr = self.collect_expr_opt(e.expr(),db);
|
||||
let expr = self.collect_expr_opt(e.expr());
|
||||
let arms = if let Some(match_arm_list) = e.match_arm_list() {
|
||||
match_arm_list
|
||||
.arms()
|
||||
.map(|arm| MatchArm {
|
||||
pats: arm.pats().map(|p| self.collect_pat(p)).collect(),
|
||||
expr: self.collect_expr_opt(arm.expr(),db),
|
||||
expr: self.collect_expr_opt(arm.expr()),
|
||||
guard: arm
|
||||
.guard()
|
||||
.and_then(|guard| guard.expr())
|
||||
.map(|e| self.collect_expr(e,db)),
|
||||
.map(|e| self.collect_expr(e)),
|
||||
})
|
||||
.collect()
|
||||
} else {
|
||||
|
@ -638,17 +629,17 @@ impl ExprCollector{
|
|||
self.alloc_expr(Expr::Continue, syntax_ptr)
|
||||
}
|
||||
ast::ExprKind::BreakExpr(e) => {
|
||||
let expr = e.expr().map(|e| self.collect_expr(e,db));
|
||||
let expr = e.expr().map(|e| self.collect_expr(e));
|
||||
self.alloc_expr(Expr::Break { expr }, syntax_ptr)
|
||||
}
|
||||
ast::ExprKind::ParenExpr(e) => {
|
||||
let inner = self.collect_expr_opt(e.expr(),db);
|
||||
let inner = self.collect_expr_opt(e.expr());
|
||||
// make the paren expr point to the inner expression as well
|
||||
self.source_map.expr_map.insert(syntax_ptr, inner);
|
||||
inner
|
||||
}
|
||||
ast::ExprKind::ReturnExpr(e) => {
|
||||
let expr = e.expr().map(|e| self.collect_expr(e,db));
|
||||
let expr = e.expr().map(|e| self.collect_expr(e));
|
||||
self.alloc_expr(Expr::Return { expr }, syntax_ptr)
|
||||
}
|
||||
ast::ExprKind::StructLit(e) => {
|
||||
|
@ -663,7 +654,7 @@ impl ExprCollector{
|
|||
.map(|nr| nr.as_name())
|
||||
.unwrap_or_else(Name::missing),
|
||||
expr: if let Some(e) = field.expr() {
|
||||
self.collect_expr(e,db)
|
||||
self.collect_expr(e)
|
||||
} else if let Some(nr) = field.name_ref() {
|
||||
// field shorthand
|
||||
let id = self.exprs.alloc(Expr::Path(Path::from_name_ref(nr)));
|
||||
|
@ -682,7 +673,7 @@ impl ExprCollector{
|
|||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
let spread = e.spread().map(|s| self.collect_expr(s,db));
|
||||
let spread = e.spread().map(|s| self.collect_expr(s));
|
||||
let res = self.alloc_expr(Expr::StructLit { path, fields, spread }, syntax_ptr);
|
||||
for (i, ptr) in field_ptrs.into_iter().enumerate() {
|
||||
self.source_map.field_map.insert((res, i), ptr);
|
||||
|
@ -690,7 +681,7 @@ impl ExprCollector{
|
|||
res
|
||||
}
|
||||
ast::ExprKind::FieldExpr(e) => {
|
||||
let expr = self.collect_expr_opt(e.expr(),db);
|
||||
let expr = self.collect_expr_opt(e.expr());
|
||||
let name = match e.field_access() {
|
||||
Some(kind) => kind.as_name(),
|
||||
_ => Name::missing(),
|
||||
|
@ -698,21 +689,21 @@ impl ExprCollector{
|
|||
self.alloc_expr(Expr::Field { expr, name }, syntax_ptr)
|
||||
}
|
||||
ast::ExprKind::TryExpr(e) => {
|
||||
let expr = self.collect_expr_opt(e.expr(),db);
|
||||
let expr = self.collect_expr_opt(e.expr());
|
||||
self.alloc_expr(Expr::Try { expr }, syntax_ptr)
|
||||
}
|
||||
ast::ExprKind::CastExpr(e) => {
|
||||
let expr = self.collect_expr_opt(e.expr(),db);
|
||||
let expr = self.collect_expr_opt(e.expr());
|
||||
let type_ref = TypeRef::from_ast_opt(e.type_ref());
|
||||
self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr)
|
||||
}
|
||||
ast::ExprKind::RefExpr(e) => {
|
||||
let expr = self.collect_expr_opt(e.expr(),db);
|
||||
let expr = self.collect_expr_opt(e.expr());
|
||||
let mutability = Mutability::from_mutable(e.is_mut());
|
||||
self.alloc_expr(Expr::Ref { expr, mutability }, syntax_ptr)
|
||||
}
|
||||
ast::ExprKind::PrefixExpr(e) => {
|
||||
let expr = self.collect_expr_opt(e.expr(),db);
|
||||
let expr = self.collect_expr_opt(e.expr());
|
||||
if let Some(op) = e.op_kind() {
|
||||
self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr)
|
||||
} else {
|
||||
|
@ -730,17 +721,17 @@ impl ExprCollector{
|
|||
arg_types.push(type_ref);
|
||||
}
|
||||
}
|
||||
let body = self.collect_expr_opt(e.body(),db);
|
||||
let body = self.collect_expr_opt(e.body());
|
||||
self.alloc_expr(Expr::Lambda { args, arg_types, body }, syntax_ptr)
|
||||
}
|
||||
ast::ExprKind::BinExpr(e) => {
|
||||
let lhs = self.collect_expr_opt(e.lhs(),db);
|
||||
let rhs = self.collect_expr_opt(e.rhs(),db);
|
||||
let lhs = self.collect_expr_opt(e.lhs());
|
||||
let rhs = self.collect_expr_opt(e.rhs());
|
||||
let op = e.op_kind();
|
||||
self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr)
|
||||
}
|
||||
ast::ExprKind::TupleExpr(e) => {
|
||||
let exprs = e.exprs().map(|expr| self.collect_expr(expr,db)).collect();
|
||||
let exprs = e.exprs().map(|expr| self.collect_expr(expr)).collect();
|
||||
self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr)
|
||||
}
|
||||
|
||||
|
@ -749,12 +740,12 @@ impl ExprCollector{
|
|||
|
||||
match kind {
|
||||
ArrayExprKind::ElementList(e) => {
|
||||
let exprs = e.map(|expr| self.collect_expr(expr,db)).collect();
|
||||
let exprs = e.map(|expr| self.collect_expr(expr)).collect();
|
||||
self.alloc_expr(Expr::Array(Array::ElementList(exprs)), syntax_ptr)
|
||||
}
|
||||
ArrayExprKind::Repeat { initializer, repeat } => {
|
||||
let initializer = self.collect_expr_opt(initializer,db);
|
||||
let repeat = self.collect_expr_opt(repeat,db);
|
||||
let initializer = self.collect_expr_opt(initializer);
|
||||
let repeat = self.collect_expr_opt(repeat);
|
||||
self.alloc_expr(
|
||||
Expr::Array(Array::Repeat { initializer, repeat }),
|
||||
syntax_ptr,
|
||||
|
@ -799,52 +790,79 @@ impl ExprCollector{
|
|||
ast::ExprKind::IndexExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
|
||||
ast::ExprKind::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
|
||||
ast::ExprKind::MacroCall(e) => {
|
||||
// very hacky.TODO change to use the macro resolution
|
||||
let name = e
|
||||
.path()
|
||||
.and_then(Path::from_ast)
|
||||
.and_then(|path| path.expand_macro_expr())
|
||||
.unwrap_or_else(Name::missing);
|
||||
|
||||
let name = e.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
|
||||
if let Some(macro_id) = self.resolver.resolve_macro_call(&name) {
|
||||
if let Some((module, _)) = self.resolver.module() {
|
||||
// we do this to get the ast_id for the macro call
|
||||
// if we used the ast_id from the macro_id variable
|
||||
// it gives us the ast_id of the defenition site
|
||||
let module = module.mk_module(module.root());
|
||||
let hir_file_id = module.definition_source(self.db).0;
|
||||
let ast_id =
|
||||
self.db.ast_id_map(hir_file_id).ast_id(e).with_file_id(hir_file_id);
|
||||
|
||||
let res = self.resolver.resolve_name(db,&name);
|
||||
let call_loc = MacroCallLoc { def: *macro_id, ast_id };
|
||||
let call_id = call_loc.id(self.db);
|
||||
let file_id: HirFileId = call_id.into();
|
||||
|
||||
// match res {
|
||||
log::debug!(
|
||||
"expanded macro ast {}",
|
||||
self.db.hir_parse(file_id).syntax().debug_dump()
|
||||
);
|
||||
|
||||
// }
|
||||
|
||||
// let resolver = Resolver
|
||||
|
||||
self.alloc_expr(Expr::Missing, syntax_ptr)
|
||||
},
|
||||
self.db
|
||||
.hir_parse(file_id)
|
||||
.syntax()
|
||||
.descendants()
|
||||
.find_map(ast::Expr::cast)
|
||||
.map(|expr| self.collect_expr(expr))
|
||||
.unwrap_or(self.alloc_expr(Expr::Missing, syntax_ptr))
|
||||
} else {
|
||||
self.alloc_expr(Expr::Missing, syntax_ptr)
|
||||
}
|
||||
} else {
|
||||
self.alloc_expr(Expr::Missing, syntax_ptr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn collect_expr_opt(&mut self, expr: Option<&ast::Expr>,db:&impl HirDatabase) -> ExprId {
|
||||
fn collect_expr_opt(&mut self, expr: Option<&ast::Expr>) -> ExprId {
|
||||
if let Some(expr) = expr {
|
||||
self.collect_expr(expr,db)
|
||||
self.collect_expr(expr)
|
||||
} else {
|
||||
self.exprs.alloc(Expr::Missing)
|
||||
}
|
||||
}
|
||||
|
||||
fn collect_block(&mut self, block: &ast::Block,db:&impl HirDatabase) -> ExprId {
|
||||
fn collect_block(&mut self, block: &ast::Block) -> ExprId {
|
||||
let statements = block
|
||||
.statements()
|
||||
.map(|s| match s.kind() {
|
||||
ast::StmtKind::LetStmt(stmt) => {
|
||||
let pat = self.collect_pat_opt(stmt.pat());
|
||||
let type_ref = stmt.ascribed_type().map(TypeRef::from_ast);
|
||||
let initializer = stmt.initializer().map(|e| self.collect_expr(e,db));
|
||||
let initializer = stmt.initializer().map(|e| self.collect_expr(e));
|
||||
Statement::Let { pat, type_ref, initializer }
|
||||
}
|
||||
ast::StmtKind::ExprStmt(stmt) => {
|
||||
Statement::Expr(self.collect_expr_opt(stmt.expr(),db))
|
||||
Statement::Expr(self.collect_expr_opt(stmt.expr()))
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let tail = block.expr().map(|e| self.collect_expr(e,db));
|
||||
let tail = block.expr().map(|e| self.collect_expr(e));
|
||||
self.alloc_expr(Expr::Block { statements, tail }, SyntaxNodePtr::new(block.syntax()))
|
||||
}
|
||||
|
||||
fn collect_block_opt(&mut self, block: Option<&ast::Block>,db:&impl HirDatabase) -> ExprId {
|
||||
fn collect_block_opt(&mut self, block: Option<&ast::Block>) -> ExprId {
|
||||
if let Some(block) = block {
|
||||
self.collect_block(block,db)
|
||||
self.collect_block(block)
|
||||
} else {
|
||||
self.exprs.alloc(Expr::Missing)
|
||||
}
|
||||
|
@ -917,17 +935,17 @@ impl ExprCollector{
|
|||
}
|
||||
}
|
||||
|
||||
fn collect_const_body(&mut self, node: &ast::ConstDef,db:&impl HirDatabase) {
|
||||
let body = self.collect_expr_opt(node.body(),db);
|
||||
fn collect_const_body(&mut self, node: &ast::ConstDef) {
|
||||
let body = self.collect_expr_opt(node.body());
|
||||
self.body_expr = Some(body);
|
||||
}
|
||||
|
||||
fn collect_static_body(&mut self, node: &ast::StaticDef,db:&impl HirDatabase) {
|
||||
let body = self.collect_expr_opt(node.body(),db);
|
||||
fn collect_static_body(&mut self, node: &ast::StaticDef) {
|
||||
let body = self.collect_expr_opt(node.body());
|
||||
self.body_expr = Some(body);
|
||||
}
|
||||
|
||||
fn collect_fn_body(&mut self, node: &ast::FnDef,db:&impl HirDatabase) {
|
||||
fn collect_fn_body(&mut self, node: &ast::FnDef) {
|
||||
if let Some(param_list) = node.param_list() {
|
||||
if let Some(self_param) = param_list.self_param() {
|
||||
let ptr = AstPtr::new(self_param);
|
||||
|
@ -953,7 +971,7 @@ impl ExprCollector{
|
|||
}
|
||||
};
|
||||
|
||||
let body = self.collect_block_opt(node.body(),db);
|
||||
let body = self.collect_block_opt(node.body());
|
||||
self.body_expr = Some(body);
|
||||
}
|
||||
|
||||
|
@ -973,14 +991,21 @@ pub(crate) fn body_with_source_map_query(
|
|||
db: &impl HirDatabase,
|
||||
def: DefWithBody,
|
||||
) -> (Arc<Body>, Arc<BodySourceMap>) {
|
||||
|
||||
let mut resolver = def.resolver(db);
|
||||
let mut collector = ExprCollector::new(def,resolver);
|
||||
let mut collector = ExprCollector {
|
||||
db,
|
||||
owner: def,
|
||||
resolver: def.resolver(db),
|
||||
exprs: Arena::default(),
|
||||
pats: Arena::default(),
|
||||
source_map: BodySourceMap::default(),
|
||||
params: Vec::new(),
|
||||
body_expr: None,
|
||||
};
|
||||
|
||||
match def {
|
||||
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,db),
|
||||
DefWithBody::Static(ref s) => collector.collect_static_body(&s.source(db).1,db),
|
||||
DefWithBody::Const(ref c) => collector.collect_const_body(&c.source(db).1),
|
||||
DefWithBody::Function(ref f) => collector.collect_fn_body(&f.source(db).1),
|
||||
DefWithBody::Static(ref s) => collector.collect_static_body(&s.source(db).1),
|
||||
}
|
||||
|
||||
let (body, source_map) = collector.finish();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue