mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-15 17:28:09 +00:00
move more macros to hir
This commit is contained in:
parent
57030f587b
commit
b2fec18098
8 changed files with 127 additions and 85 deletions
|
@ -1,7 +1,6 @@
|
|||
use std::{fmt, sync::Arc};
|
||||
use salsa::{self, Database};
|
||||
use ra_db::{LocationIntener, BaseDatabase};
|
||||
use hir::{self, DefId, DefLoc};
|
||||
|
||||
use crate::{
|
||||
symbol_index,
|
||||
|
@ -15,7 +14,8 @@ pub(crate) struct RootDatabase {
|
|||
|
||||
#[derive(Default)]
|
||||
struct IdMaps {
|
||||
defs: LocationIntener<DefLoc, DefId>,
|
||||
defs: LocationIntener<hir::DefLoc, hir::DefId>,
|
||||
macros: LocationIntener<hir::MacroInvocationLoc, hir::MacroInvocationId>,
|
||||
}
|
||||
|
||||
impl fmt::Debug for IdMaps {
|
||||
|
@ -59,12 +59,18 @@ impl salsa::ParallelDatabase for RootDatabase {
|
|||
|
||||
impl BaseDatabase for RootDatabase {}
|
||||
|
||||
impl AsRef<LocationIntener<DefLoc, DefId>> for RootDatabase {
|
||||
fn as_ref(&self) -> &LocationIntener<DefLoc, DefId> {
|
||||
impl AsRef<LocationIntener<hir::DefLoc, hir::DefId>> for RootDatabase {
|
||||
fn as_ref(&self) -> &LocationIntener<hir::DefLoc, hir::DefId> {
|
||||
&self.id_maps.defs
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<LocationIntener<hir::MacroInvocationLoc, hir::MacroInvocationId>> for RootDatabase {
|
||||
fn as_ref(&self) -> &LocationIntener<hir::MacroInvocationLoc, hir::MacroInvocationId> {
|
||||
&self.id_maps.macros
|
||||
}
|
||||
}
|
||||
|
||||
salsa::database_storage! {
|
||||
pub(crate) struct RootDatabaseStorage for RootDatabase {
|
||||
impl ra_db::FilesDatabase {
|
||||
|
@ -85,6 +91,7 @@ salsa::database_storage! {
|
|||
fn library_symbols() for symbol_index::LibrarySymbolsQuery;
|
||||
}
|
||||
impl hir::db::HirDatabase {
|
||||
fn expand_macro_invocation() for hir::db::ExpandMacroInvocationQuery;
|
||||
fn module_tree() for hir::db::ModuleTreeQuery;
|
||||
fn fn_scopes() for hir::db::FnScopesQuery;
|
||||
fn file_items() for hir::db::SourceFileItemsQuery;
|
||||
|
@ -98,8 +105,5 @@ salsa::database_storage! {
|
|||
fn struct_data() for hir::db::StructDataQuery;
|
||||
fn enum_data() for hir::db::EnumDataQuery;
|
||||
}
|
||||
impl hir::MacroDatabase {
|
||||
fn expand_macro() for hir::ExpandMacroQuery;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,12 +18,12 @@ pub(crate) fn extend_selection(db: &RootDatabase, frange: FileRange) -> TextRang
|
|||
}
|
||||
|
||||
fn extend_selection_in_macro(
|
||||
db: &RootDatabase,
|
||||
_db: &RootDatabase,
|
||||
source_file: &SourceFileNode,
|
||||
frange: FileRange,
|
||||
) -> Option<TextRange> {
|
||||
let macro_call = find_macro_call(source_file.syntax(), frange.range)?;
|
||||
let (off, exp) = crate::macros::expand(db, frange.file_id, macro_call)?;
|
||||
let (off, exp) = hir::MacroDef::ast_expand(macro_call)?;
|
||||
let dst_range = exp.map_range_forward(frange.range - off)?;
|
||||
let dst_range = ra_editor::extend_selection(exp.syntax().borrowed(), dst_range)?;
|
||||
let src_range = exp.map_range_back(dst_range)? + off;
|
||||
|
|
|
@ -19,7 +19,6 @@ mod runnables;
|
|||
|
||||
mod extend_selection;
|
||||
mod syntax_highlighting;
|
||||
mod macros;
|
||||
|
||||
use std::{fmt, sync::Arc};
|
||||
|
||||
|
|
|
@ -1,32 +0,0 @@
|
|||
/// Begining of macro expansion.
|
||||
///
|
||||
/// This code should be moved out of ra_analysis into hir (?) ideally.
|
||||
use std::sync::Arc;
|
||||
|
||||
use ra_syntax::{ast, AstNode, TextUnit};
|
||||
use hir::MacroDatabase;
|
||||
|
||||
use crate::{db::RootDatabase, FileId};
|
||||
|
||||
pub(crate) fn expand(
|
||||
db: &RootDatabase,
|
||||
_file_id: FileId,
|
||||
macro_call: ast::MacroCall,
|
||||
) -> Option<(TextUnit, Arc<hir::MacroExpansion>)> {
|
||||
let path = macro_call.path()?;
|
||||
if path.qualifier().is_some() {
|
||||
return None;
|
||||
}
|
||||
let name_ref = path.segment()?.name_ref()?;
|
||||
if name_ref.text() != "ctry" {
|
||||
return None;
|
||||
}
|
||||
let arg = macro_call.token_tree()?.syntax();
|
||||
|
||||
let def = hir::MacroDef::CTry;
|
||||
let input = hir::MacroInput {
|
||||
text: arg.text().to_string(),
|
||||
};
|
||||
let exp = db.expand_macro(def, input)?;
|
||||
Some((arg.range().start(), exp))
|
||||
}
|
|
@ -15,7 +15,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Cancelable<Vec<Hi
|
|||
.descendants()
|
||||
.filter_map(ast::MacroCall::cast)
|
||||
{
|
||||
if let Some((off, exp)) = crate::macros::expand(db, file_id, macro_call) {
|
||||
if let Some((off, exp)) = hir::MacroDef::ast_expand(macro_call) {
|
||||
let mapped_ranges = ra_editor::highlight(exp.syntax().borrowed())
|
||||
.into_iter()
|
||||
.filter_map(|r| {
|
||||
|
|
|
@ -8,6 +8,7 @@ use crate::{
|
|||
SourceFileItems, SourceItemId,
|
||||
query_definitions,
|
||||
FnScopes,
|
||||
macros::{MacroInvocationLoc, MacroInvocationId, MacroInput, MacroDef, MacroExpansion},
|
||||
module::{ModuleId, ModuleTree, ModuleSource,
|
||||
nameres::{ItemMap, InputModuleItems}},
|
||||
ty::{InferenceResult, Ty},
|
||||
|
@ -18,7 +19,13 @@ salsa::query_group! {
|
|||
|
||||
pub trait HirDatabase: SyntaxDatabase
|
||||
+ AsRef<LocationIntener<DefLoc, DefId>>
|
||||
+ AsRef<LocationIntener<MacroInvocationLoc, MacroInvocationId>>
|
||||
{
|
||||
fn expand_macro_invocation(invoc: MacroInvocationId) -> Option<Arc<MacroExpansion>> {
|
||||
type ExpandMacroInvocationQuery;
|
||||
use fn crate::macros::expand_macro_invocation;
|
||||
}
|
||||
|
||||
fn fn_scopes(def_id: DefId) -> Arc<FnScopes> {
|
||||
type FnScopesQuery;
|
||||
use fn query_definitions::fn_scopes;
|
||||
|
|
|
@ -46,7 +46,7 @@ pub use self::{
|
|||
path::{Path, PathKind},
|
||||
name::Name,
|
||||
krate::Crate,
|
||||
macros::{MacroDef, MacroInput, MacroExpansion, MacroDatabase, ExpandMacroQuery},
|
||||
macros::{MacroDef, MacroInput, MacroExpansion, MacroInvocationId, MacroInvocationLoc},
|
||||
module::{Module, ModuleId, Problem, nameres::{ItemMap, PerNs, Namespace}, ModuleScope, Resolution},
|
||||
function::{Function, FnScopes},
|
||||
adt::{Struct, Enum},
|
||||
|
|
|
@ -1,17 +1,108 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use ra_db::{SyntaxDatabase, LocalSyntaxPtr};
|
||||
use ra_db::{LocalSyntaxPtr, LocationIntener};
|
||||
use ra_syntax::{
|
||||
TextRange, TextUnit, SourceFileNode, AstNode, SyntaxNode,
|
||||
ast,
|
||||
};
|
||||
|
||||
use crate::{SourceRootId, module::ModuleId, SourceItemId, HirDatabase};
|
||||
|
||||
/// Def's are a core concept of hir. A `Def` is an Item (function, module, etc)
|
||||
/// in a specific module.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct MacroInvocationId(u32);
|
||||
ra_db::impl_numeric_id!(MacroInvocationId);
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct MacroInvocationLoc {
|
||||
source_root_id: SourceRootId,
|
||||
module_id: ModuleId,
|
||||
source_item_id: SourceItemId,
|
||||
}
|
||||
|
||||
impl MacroInvocationId {
|
||||
pub(crate) fn loc(
|
||||
self,
|
||||
db: &impl AsRef<LocationIntener<MacroInvocationLoc, MacroInvocationId>>,
|
||||
) -> MacroInvocationLoc {
|
||||
db.as_ref().id2loc(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl MacroInvocationLoc {
|
||||
#[allow(unused)]
|
||||
pub(crate) fn id(
|
||||
&self,
|
||||
db: &impl AsRef<LocationIntener<MacroInvocationLoc, MacroInvocationId>>,
|
||||
) -> MacroInvocationId {
|
||||
db.as_ref().loc2id(&self)
|
||||
}
|
||||
}
|
||||
|
||||
// Hard-coded defs for now :-(
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum MacroDef {
|
||||
CTry,
|
||||
}
|
||||
|
||||
impl MacroDef {
|
||||
pub fn ast_expand(macro_call: ast::MacroCall) -> Option<(TextUnit, MacroExpansion)> {
|
||||
let (def, input) = MacroDef::from_call(macro_call)?;
|
||||
let exp = def.expand(input)?;
|
||||
let off = macro_call.token_tree()?.syntax().range().start();
|
||||
Some((off, exp))
|
||||
}
|
||||
|
||||
fn from_call(macro_call: ast::MacroCall) -> Option<(MacroDef, MacroInput)> {
|
||||
let def = {
|
||||
let path = macro_call.path()?;
|
||||
if path.qualifier().is_some() {
|
||||
return None;
|
||||
}
|
||||
let name_ref = path.segment()?.name_ref()?;
|
||||
if name_ref.text() != "ctry" {
|
||||
return None;
|
||||
}
|
||||
MacroDef::CTry
|
||||
};
|
||||
|
||||
let input = {
|
||||
let arg = macro_call.token_tree()?.syntax();
|
||||
MacroInput {
|
||||
text: arg.text().to_string(),
|
||||
}
|
||||
};
|
||||
Some((def, input))
|
||||
}
|
||||
|
||||
fn expand(self, input: MacroInput) -> Option<MacroExpansion> {
|
||||
let MacroDef::CTry = self;
|
||||
let text = format!(
|
||||
r"
|
||||
fn dummy() {{
|
||||
match {} {{
|
||||
None => return Ok(None),
|
||||
Some(it) => it,
|
||||
}}
|
||||
}}",
|
||||
input.text
|
||||
);
|
||||
let file = SourceFileNode::parse(&text);
|
||||
let match_expr = file.syntax().descendants().find_map(ast::MatchExpr::cast)?;
|
||||
let match_arg = match_expr.expr()?;
|
||||
let ptr = LocalSyntaxPtr::new(match_arg.syntax());
|
||||
let src_range = TextRange::offset_len(0.into(), TextUnit::of_str(&input.text));
|
||||
let ranges_map = vec![(src_range, match_arg.syntax().range())];
|
||||
let res = MacroExpansion {
|
||||
text,
|
||||
ranges_map,
|
||||
ptr,
|
||||
};
|
||||
Some(res)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct MacroInput {
|
||||
// Should be token trees
|
||||
|
@ -25,46 +116,6 @@ pub struct MacroExpansion {
|
|||
ptr: LocalSyntaxPtr,
|
||||
}
|
||||
|
||||
salsa::query_group! {
|
||||
|
||||
pub trait MacroDatabase: SyntaxDatabase {
|
||||
fn expand_macro(def: MacroDef, input: MacroInput) -> Option<Arc<MacroExpansion>> {
|
||||
type ExpandMacroQuery;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn expand_macro(
|
||||
_db: &impl MacroDatabase,
|
||||
def: MacroDef,
|
||||
input: MacroInput,
|
||||
) -> Option<Arc<MacroExpansion>> {
|
||||
let MacroDef::CTry = def;
|
||||
let text = format!(
|
||||
r"
|
||||
fn dummy() {{
|
||||
match {} {{
|
||||
None => return Ok(None),
|
||||
Some(it) => it,
|
||||
}}
|
||||
}}",
|
||||
input.text
|
||||
);
|
||||
let file = SourceFileNode::parse(&text);
|
||||
let match_expr = file.syntax().descendants().find_map(ast::MatchExpr::cast)?;
|
||||
let match_arg = match_expr.expr()?;
|
||||
let ptr = LocalSyntaxPtr::new(match_arg.syntax());
|
||||
let src_range = TextRange::offset_len(0.into(), TextUnit::of_str(&input.text));
|
||||
let ranges_map = vec![(src_range, match_arg.syntax().range())];
|
||||
let res = MacroExpansion {
|
||||
text,
|
||||
ranges_map,
|
||||
ptr,
|
||||
};
|
||||
Some(Arc::new(res))
|
||||
}
|
||||
|
||||
impl MacroExpansion {
|
||||
pub fn file(&self) -> SourceFileNode {
|
||||
SourceFileNode::parse(&self.text)
|
||||
|
@ -96,3 +147,16 @@ impl MacroExpansion {
|
|||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn expand_macro_invocation(
|
||||
db: &impl HirDatabase,
|
||||
invoc: MacroInvocationId,
|
||||
) -> Option<Arc<MacroExpansion>> {
|
||||
let loc = invoc.loc(db);
|
||||
let syntax = db.file_item(loc.source_item_id);
|
||||
let syntax = syntax.borrowed();
|
||||
let macro_call = ast::MacroCall::cast(syntax).unwrap();
|
||||
|
||||
let (def, input) = MacroDef::from_call(macro_call)?;
|
||||
def.expand(input).map(Arc::new)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue