From ce211434a6501e88cb83462f2443db085f1557d3 Mon Sep 17 00:00:00 2001 From: Lenard Pratt Date: Mon, 15 Apr 2019 11:01:29 +0100 Subject: Added macro resolution and expansion --- .vscode/launch.json | 4 +- crates/ra_hir/src/expr.rs | 201 ++++++++++++++++------------- crates/ra_hir/src/ids.rs | 2 +- crates/ra_hir/src/nameres.rs | 6 + crates/ra_hir/src/nameres/collector.rs | 8 +- crates/ra_hir/src/path.rs | 4 + crates/ra_hir/src/resolve.rs | 7 +- crates/ra_parser/src/grammar/Untitled-1.rs | 13 -- 8 files changed, 138 insertions(+), 107 deletions(-) delete mode 100644 crates/ra_parser/src/grammar/Untitled-1.rs diff --git a/.vscode/launch.json b/.vscode/launch.json index 9aafc8bd3..1515ea411 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -14,7 +14,9 @@ "--disable-extensions" ], "env": { - "__RA_LSP_SERVER_DEBUG": "${workspaceFolder}/target/debug/ra_lsp_server" + "__RA_LSP_SERVER_DEBUG": "${workspaceFolder}/target/debug/ra_lsp_server", + "RUST_LOG" :"ra_hir=debug", + "RA_INTERNAL_MODE":"1" }, "outFiles": ["${workspaceFolder}/editors/code/out/**/*.js"], "preLaunchTask": "Build All" diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 7759a6d4f..7d5257461 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -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, owner: DefWithBody, exprs: Arena, pats: Arena, @@ -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) => { - - 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) - }, + // 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); + + 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 call_loc = MacroCallLoc { def: *macro_id, ast_id }; + let call_id = call_loc.id(self.db); + let file_id: HirFileId = call_id.into(); + + log::debug!( + "expanded macro ast {}", + self.db.hir_parse(file_id).syntax().debug_dump() + ); + + 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, Arc) { - - 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(); diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index c7849c995..a07624a19 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs @@ -101,7 +101,7 @@ fn parse_macro( return Err(format!("Total tokens count exceed limit : count = {}", count)); } - Ok(mbe::token_tree_to_ast_item_list(&tt)) + Some(mbe::token_tree_to_ast_item_list(&tt)) } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index fbfff4fd7..a450d7b84 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -104,6 +104,7 @@ pub struct CrateDefMap { /// However, do we want to put it as a global variable? poison_macros: FxHashSet, + local_macros: FxHashMap, diagnostics: Vec, } @@ -209,6 +210,7 @@ impl CrateDefMap { modules, public_macros: FxHashMap::default(), poison_macros: FxHashSet::default(), + local_macros: FxHashMap::default(), diagnostics: Vec::new(), } }; @@ -270,6 +272,10 @@ impl CrateDefMap { (res.resolved_def, res.segment_index) } + pub(crate) fn find_macro(&self, name: &Name) -> Option<&MacroDefId> { + self.public_macros.get(name).or(self.local_macros.get(name)) + } + // Returns Yes if we are sure that additions to `ItemMap` wouldn't change // the result. fn resolve_path_fp( diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs index 4590a5184..762a61604 100644 --- a/crates/ra_hir/src/nameres/collector.rs +++ b/crates/ra_hir/src/nameres/collector.rs @@ -131,6 +131,8 @@ where fn define_macro(&mut self, name: Name, macro_id: MacroDefId, export: bool) { if export { self.def_map.public_macros.insert(name.clone(), macro_id); + } else { + self.def_map.local_macros.insert(name.clone(), macro_id); } self.global_macro_scope.insert(name, macro_id); } @@ -517,10 +519,10 @@ where // Case 2: try to expand macro_rules from this crate, triggering // recursive item collection. - if let Some(¯o_id) = - mac.path.as_ident().and_then(|name| self.def_collector.global_macro_scope.get(name)) + if let Some(macro_id) = + mac.path.as_ident().and_then(|name| self.def_collector.global_macro_scope.get(&name)) { - let macro_call_id = MacroCallLoc { def: macro_id, ast_id }.id(self.def_collector.db); + let macro_call_id = MacroCallLoc { def: *macro_id, ast_id }.id(self.def_collector.db); self.def_collector.collect_macro_expansion(self.module_id, macro_call_id, macro_id); return; diff --git a/crates/ra_hir/src/path.rs b/crates/ra_hir/src/path.rs index 5449cddfd..1b129c752 100644 --- a/crates/ra_hir/src/path.rs +++ b/crates/ra_hir/src/path.rs @@ -126,6 +126,10 @@ impl Path { } self.segments.first().map(|s| &s.name) } + + pub fn expand_macro_expr(&self) -> Option { + self.as_ident().and_then(|name| Some(name.clone())) + } } impl GenericArgs { diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index f2c85eb66..1def032f9 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs @@ -6,6 +6,7 @@ use rustc_hash::FxHashMap; use crate::{ ModuleDef, code_model_api::Crate, + MacroDefId, db::HirDatabase, name::{Name, KnownName}, nameres::{PerNs, CrateDefMap, CrateModuleId}, @@ -130,6 +131,10 @@ impl Resolver { resolution } + pub fn resolve_macro_call(&self, name: &Name) -> Option<&MacroDefId> { + self.module().and_then(|(module, _)| module.find_macro(name)) + } + /// Returns the resolved path segments /// Which may be fully resolved, empty or partially resolved. pub(crate) fn resolve_path_segments(&self, db: &impl HirDatabase, path: &Path) -> PathResult { @@ -192,7 +197,7 @@ impl Resolver { .flatten() } - fn module(&self) -> Option<(&CrateDefMap, CrateModuleId)> { + pub(crate) fn module(&self) -> Option<(&CrateDefMap, CrateModuleId)> { self.scopes.iter().rev().find_map(|scope| match scope { Scope::ModuleScope(m) => Some((&*m.crate_def_map, m.module_id)), diff --git a/crates/ra_parser/src/grammar/Untitled-1.rs b/crates/ra_parser/src/grammar/Untitled-1.rs deleted file mode 100644 index 2106872c8..000000000 --- a/crates/ra_parser/src/grammar/Untitled-1.rs +++ /dev/null @@ -1,13 +0,0 @@ -macro_rules! vec { - ($($item:expr),*) => - { - { - let mut v = Vec::new(); - $( - v.push($item); - )* - v - } - }; -} - -- cgit v1.2.3