rust/crates/ra_hir/src/macros.rs

198 lines
7 KiB
Rust
Raw Normal View History

2019-01-02 14:00:01 +01:00
/// Machinery for macro expansion.
///
/// One of the more complicated things about macros is managing the source code
/// that is produced after expansion. See `HirFileId` and `MacroCallId` for how
/// do we do that.
///
/// When the file-management question is resolved, all that is left is a
/// token-tree-to-token-tree transformation plus hygiene. We don't have either of
/// those yet, so all macros are string based at the moment!
2018-12-31 14:05:07 +01:00
use std::sync::Arc;
2018-12-31 17:19:50 +01:00
use ra_syntax::{
2019-01-23 15:37:10 +01:00
TextRange, TextUnit, SourceFile, AstNode, SyntaxNode, TreeArc, SyntaxNodePtr,
2019-01-01 17:23:03 +01:00
ast::{self, NameOwner},
2018-12-31 17:19:50 +01:00
};
2018-12-31 14:05:07 +01:00
2019-01-01 22:37:36 +01:00
use crate::{HirDatabase, MacroCallId};
2019-01-01 16:11:04 +01:00
2018-12-31 14:05:07 +01:00
// Hard-coded defs for now :-(
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum MacroDef {
CTry,
2019-01-03 12:44:31 +01:00
Vec,
2019-01-01 17:23:03 +01:00
QueryGroup,
2018-12-31 14:05:07 +01:00
}
2019-01-01 16:11:04 +01:00
impl MacroDef {
2019-01-02 14:00:01 +01:00
/// Expands macro call, returning the expansion and offset to be used to
/// convert ranges between expansion and original source.
2019-01-08 09:28:42 +01:00
pub fn ast_expand(macro_call: &ast::MacroCall) -> Option<(TextUnit, MacroExpansion)> {
2019-01-01 16:11:04 +01:00
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))
}
2019-01-08 09:28:42 +01:00
fn from_call(macro_call: &ast::MacroCall) -> Option<(MacroDef, MacroInput)> {
2019-01-01 16:11:04 +01:00
let def = {
let path = macro_call.path()?;
let name_ref = path.segment()?.name_ref()?;
2019-01-01 17:23:03 +01:00
if name_ref.text() == "ctry" {
MacroDef::CTry
2019-01-03 12:44:31 +01:00
} else if name_ref.text() == "vec" {
MacroDef::Vec
2019-01-01 17:23:03 +01:00
} else if name_ref.text() == "query_group" {
MacroDef::QueryGroup
} else {
2019-01-01 16:11:04 +01:00
return None;
}
};
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> {
2019-01-01 17:23:03 +01:00
match self {
MacroDef::CTry => self.expand_ctry(input),
2019-01-03 12:44:31 +01:00
MacroDef::Vec => self.expand_vec(input),
2019-01-01 17:23:03 +01:00
MacroDef::QueryGroup => self.expand_query_group(input),
}
}
fn expand_ctry(self, input: MacroInput) -> Option<MacroExpansion> {
2019-01-01 16:11:04 +01:00
let text = format!(
r"
fn dummy() {{
match {} {{
None => return Ok(None),
Some(it) => it,
}}
}}",
input.text
);
2019-01-08 09:28:42 +01:00
let file = SourceFile::parse(&text);
2019-01-01 16:11:04 +01:00
let match_expr = file.syntax().descendants().find_map(ast::MatchExpr::cast)?;
let match_arg = match_expr.expr()?;
2019-01-23 15:37:10 +01:00
let ptr = SyntaxNodePtr::new(match_arg.syntax());
2019-01-01 16:11:04 +01:00
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)
}
2019-01-03 12:44:31 +01:00
fn expand_vec(self, input: MacroInput) -> Option<MacroExpansion> {
let text = format!(r"fn dummy() {{ {}; }}", input.text);
2019-01-08 09:28:42 +01:00
let file = SourceFile::parse(&text);
2019-01-03 12:44:31 +01:00
let array_expr = file.syntax().descendants().find_map(ast::ArrayExpr::cast)?;
2019-01-23 15:37:10 +01:00
let ptr = SyntaxNodePtr::new(array_expr.syntax());
2019-01-03 12:44:31 +01:00
let src_range = TextRange::offset_len(0.into(), TextUnit::of_str(&input.text));
let ranges_map = vec![(src_range, array_expr.syntax().range())];
let res = MacroExpansion {
text,
ranges_map,
ptr,
};
Some(res)
}
2019-01-01 17:23:03 +01:00
fn expand_query_group(self, input: MacroInput) -> Option<MacroExpansion> {
let anchor = "trait ";
let pos = input.text.find(anchor)? + anchor.len();
let trait_name = input.text[pos..]
.chars()
.take_while(|c| c.is_alphabetic())
.collect::<String>();
if trait_name.is_empty() {
return None;
}
let src_range = TextRange::offset_len((pos as u32).into(), TextUnit::of_str(&trait_name));
let text = format!(r"trait {} {{ }}", trait_name);
2019-01-08 09:28:42 +01:00
let file = SourceFile::parse(&text);
2019-01-01 17:23:03 +01:00
let trait_def = file.syntax().descendants().find_map(ast::TraitDef::cast)?;
let name = trait_def.name()?;
2019-01-23 15:37:10 +01:00
let ptr = SyntaxNodePtr::new(trait_def.syntax());
2019-01-01 17:23:03 +01:00
let ranges_map = vec![(src_range, name.syntax().range())];
let res = MacroExpansion {
text,
ranges_map,
ptr,
};
Some(res)
}
2019-01-01 16:11:04 +01:00
}
2018-12-31 14:05:07 +01:00
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MacroInput {
// Should be token trees
2018-12-31 17:19:50 +01:00
pub text: String,
2018-12-31 14:05:07 +01:00
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MacroExpansion {
2019-01-02 14:00:01 +01:00
/// The result of macro expansion. Should be token tree as well.
2018-12-31 14:05:07 +01:00
text: String,
2019-01-02 14:00:01 +01:00
/// Correspondence between ranges in the original source code and ranges in
/// the macro.
2018-12-31 14:05:07 +01:00
ranges_map: Vec<(TextRange, TextRange)>,
2019-01-02 14:00:01 +01:00
/// Implementation detail: internally, a macro is expanded to the whole file,
/// even if it is an expression. This `ptr` selects the actual expansion from
/// the expanded file.
2019-01-23 15:37:10 +01:00
ptr: SyntaxNodePtr,
2018-12-31 14:05:07 +01:00
}
2018-12-31 17:19:50 +01:00
impl MacroExpansion {
2019-01-02 14:00:01 +01:00
// FIXME: does not really make sense, macro expansion is not neccessary a
// whole file. See `MacroExpansion::ptr` as well.
pub(crate) fn file(&self) -> TreeArc<SourceFile> {
2019-01-08 09:28:42 +01:00
SourceFile::parse(&self.text)
2018-12-31 17:19:50 +01:00
}
pub fn syntax(&self) -> TreeArc<SyntaxNode> {
2019-01-23 15:37:10 +01:00
self.ptr.to_node(&self.file()).to_owned()
2018-12-31 17:19:50 +01:00
}
2019-01-02 14:00:01 +01:00
/// Maps range in the source code to the range in the expanded code.
pub fn map_range_forward(&self, src_range: TextRange) -> Option<TextRange> {
2018-12-31 17:19:50 +01:00
for (s_range, t_range) in self.ranges_map.iter() {
2019-01-02 14:00:01 +01:00
if src_range.is_subrange(&s_range) {
let src_at_zero_range = src_range - src_range.start();
let src_range_offset = src_range.start() - s_range.start();
let src_range = src_at_zero_range + src_range_offset + t_range.start();
2018-12-31 17:19:50 +01:00
return Some(src_range);
}
}
None
}
2019-01-02 14:00:01 +01:00
/// Maps range in the expanded code to the range in the source code.
pub fn map_range_back(&self, tgt_range: TextRange) -> Option<TextRange> {
2018-12-31 17:19:50 +01:00
for (s_range, t_range) in self.ranges_map.iter() {
2019-01-02 14:00:01 +01:00
if tgt_range.is_subrange(&t_range) {
let tgt_at_zero_range = tgt_range - tgt_range.start();
let tgt_range_offset = tgt_range.start() - t_range.start();
let src_range = tgt_at_zero_range + tgt_range_offset + s_range.start();
2018-12-31 17:19:50 +01:00
return Some(src_range);
}
}
None
}
}
2019-01-01 16:11:04 +01:00
pub(crate) fn expand_macro_invocation(
db: &impl HirDatabase,
2019-01-01 16:12:31 +01:00
invoc: MacroCallId,
2019-01-01 16:11:04 +01:00
) -> Option<Arc<MacroExpansion>> {
let loc = invoc.loc(db);
let syntax = db.file_item(loc.source_item_id);
2019-01-08 09:28:42 +01:00
let macro_call = ast::MacroCall::cast(&syntax).unwrap();
2019-01-01 16:11:04 +01:00
let (def, input) = MacroDef::from_call(macro_call)?;
def.expand(input).map(Arc::new)
}