2019-11-09 21:03:24 -06:00
|
|
|
//! Builtin macro
|
2019-11-11 00:15:09 -06:00
|
|
|
use crate::db::AstDatabase;
|
|
|
|
use crate::{
|
|
|
|
ast::{self, AstNode},
|
2019-11-11 04:45:55 -06:00
|
|
|
name, AstId, CrateId, HirFileId, MacroCallId, MacroDefId, MacroDefKind, MacroFileKind,
|
2019-11-11 00:15:09 -06:00
|
|
|
TextUnit,
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::quote;
|
2019-11-09 21:03:24 -06:00
|
|
|
|
2019-11-22 11:47:35 -06:00
|
|
|
macro_rules! register_builtin {
|
|
|
|
( $(($name:ident, $kind: ident) => $expand:ident),* ) => {
|
2019-11-23 08:48:34 -06:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub enum BuiltinFnLikeExpander {
|
|
|
|
$($kind),*
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BuiltinFnLikeExpander {
|
|
|
|
pub fn expand(
|
|
|
|
&self,
|
|
|
|
db: &dyn AstDatabase,
|
|
|
|
id: MacroCallId,
|
|
|
|
tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
|
|
|
let expander = match *self {
|
|
|
|
$( BuiltinFnLikeExpander::$kind => $expand, )*
|
|
|
|
};
|
|
|
|
expander(db, id, tt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_builtin_macro(
|
|
|
|
ident: &name::Name,
|
|
|
|
krate: CrateId,
|
|
|
|
ast_id: AstId<ast::MacroCall>,
|
|
|
|
) -> Option<MacroDefId> {
|
|
|
|
let kind = match ident {
|
|
|
|
$( id if id == &name::$name => BuiltinFnLikeExpander::$kind, )*
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
2019-12-05 08:10:33 -06:00
|
|
|
Some(MacroDefId { krate: Some(krate), ast_id: Some(ast_id), kind: MacroDefKind::BuiltIn(kind) })
|
2019-11-23 08:48:34 -06:00
|
|
|
}
|
2019-11-22 11:47:35 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
register_builtin! {
|
|
|
|
(COLUMN_MACRO, Column) => column_expand,
|
2019-11-24 18:01:51 -06:00
|
|
|
(COMPILE_ERROR_MACRO, CompileError) => compile_error_expand,
|
2019-11-22 11:47:35 -06:00
|
|
|
(FILE_MACRO, File) => file_expand,
|
|
|
|
(LINE_MACRO, Line) => line_expand,
|
2019-12-06 03:57:20 -06:00
|
|
|
(STRINGIFY_MACRO, Stringify) => stringify_expand,
|
|
|
|
(FORMAT_ARGS_MACRO, FormatArgs) => format_args_expand,
|
|
|
|
// format_args_nl only differs in that it adds a newline in the end,
|
|
|
|
// so we use the same stub expansion for now
|
|
|
|
(FORMAT_ARGS_NL_MACRO, FormatArgsNl) => format_args_expand
|
2019-11-22 11:47:35 -06:00
|
|
|
}
|
|
|
|
|
2019-11-11 00:15:09 -06:00
|
|
|
fn to_line_number(db: &dyn AstDatabase, file: HirFileId, pos: TextUnit) -> usize {
|
|
|
|
// FIXME: Use expansion info
|
|
|
|
let file_id = file.original_file(db);
|
|
|
|
let text = db.file_text(file_id);
|
|
|
|
let mut line_num = 1;
|
|
|
|
|
2019-11-24 12:02:04 -06:00
|
|
|
let pos = pos.to_usize();
|
|
|
|
if pos > text.len() {
|
|
|
|
// FIXME: `pos` at the moment could be an offset inside the "wrong" file
|
|
|
|
// in this case, when we know it's wrong, we return a dummy value
|
|
|
|
return 0;
|
|
|
|
}
|
2019-11-11 00:15:09 -06:00
|
|
|
// Count line end
|
|
|
|
for (i, c) in text.chars().enumerate() {
|
2019-11-24 12:02:04 -06:00
|
|
|
if i == pos {
|
2019-11-11 00:15:09 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if c == '\n' {
|
|
|
|
line_num += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
line_num
|
|
|
|
}
|
|
|
|
|
|
|
|
fn line_expand(
|
|
|
|
db: &dyn AstDatabase,
|
|
|
|
id: MacroCallId,
|
|
|
|
_tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
|
|
|
let loc = db.lookup_intern_macro(id);
|
|
|
|
|
2019-12-05 08:10:33 -06:00
|
|
|
let arg = loc.kind.arg(db).ok_or_else(|| mbe::ExpandError::UnexpectedToken)?;
|
|
|
|
let arg_start = arg.text_range().start();
|
2019-11-11 00:15:09 -06:00
|
|
|
|
|
|
|
let file = id.as_file(MacroFileKind::Expr);
|
|
|
|
let line_num = to_line_number(db, file, arg_start);
|
|
|
|
|
|
|
|
let expanded = quote! {
|
|
|
|
#line_num
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(expanded)
|
|
|
|
}
|
2019-11-22 00:56:50 -06:00
|
|
|
|
|
|
|
fn stringify_expand(
|
|
|
|
db: &dyn AstDatabase,
|
|
|
|
id: MacroCallId,
|
|
|
|
_tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
|
|
|
let loc = db.lookup_intern_macro(id);
|
|
|
|
|
|
|
|
let macro_content = {
|
2019-12-05 08:10:33 -06:00
|
|
|
let arg = loc.kind.arg(db).ok_or_else(|| mbe::ExpandError::UnexpectedToken)?;
|
|
|
|
let macro_args = arg.clone();
|
2019-11-22 00:56:50 -06:00
|
|
|
let text = macro_args.text();
|
|
|
|
let without_parens = TextUnit::of_char('(')..text.len() - TextUnit::of_char(')');
|
|
|
|
text.slice(without_parens).to_string()
|
|
|
|
};
|
|
|
|
|
|
|
|
let expanded = quote! {
|
|
|
|
#macro_content
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(expanded)
|
|
|
|
}
|
2019-11-22 09:05:04 -06:00
|
|
|
|
2019-11-22 07:48:33 -06:00
|
|
|
fn to_col_number(db: &dyn AstDatabase, file: HirFileId, pos: TextUnit) -> usize {
|
|
|
|
// FIXME: Use expansion info
|
|
|
|
let file_id = file.original_file(db);
|
|
|
|
let text = db.file_text(file_id);
|
|
|
|
|
2019-11-24 12:02:04 -06:00
|
|
|
let pos = pos.to_usize();
|
|
|
|
if pos > text.len() {
|
|
|
|
// FIXME: `pos` at the moment could be an offset inside the "wrong" file
|
|
|
|
// in this case we return a dummy value so that we don't `panic!`
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut col_num = 1;
|
|
|
|
for c in text[..pos].chars().rev() {
|
2019-11-22 07:48:33 -06:00
|
|
|
if c == '\n' {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
col_num = col_num + 1;
|
|
|
|
}
|
|
|
|
col_num
|
|
|
|
}
|
|
|
|
|
|
|
|
fn column_expand(
|
|
|
|
db: &dyn AstDatabase,
|
|
|
|
id: MacroCallId,
|
|
|
|
_tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
|
|
|
let loc = db.lookup_intern_macro(id);
|
2019-12-05 08:10:33 -06:00
|
|
|
let macro_call = match loc.kind {
|
|
|
|
crate::MacroCallKind::FnLike(ast_id) => ast_id.to_node(db),
|
|
|
|
_ => panic!("column macro called as attr"),
|
|
|
|
};
|
2019-11-22 07:48:33 -06:00
|
|
|
|
|
|
|
let _arg = macro_call.token_tree().ok_or_else(|| mbe::ExpandError::UnexpectedToken)?;
|
|
|
|
let col_start = macro_call.syntax().text_range().start();
|
|
|
|
|
|
|
|
let file = id.as_file(MacroFileKind::Expr);
|
|
|
|
let col_num = to_col_number(db, file, col_start);
|
|
|
|
|
|
|
|
let expanded = quote! {
|
|
|
|
#col_num
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(expanded)
|
|
|
|
}
|
|
|
|
|
2019-11-22 09:05:04 -06:00
|
|
|
fn file_expand(
|
2019-12-05 08:10:33 -06:00
|
|
|
_db: &dyn AstDatabase,
|
|
|
|
_id: MacroCallId,
|
2019-11-22 09:05:04 -06:00
|
|
|
_tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
|
|
|
// FIXME: RA purposefully lacks knowledge of absolute file names
|
|
|
|
// so just return "".
|
|
|
|
let file_name = "";
|
|
|
|
|
|
|
|
let expanded = quote! {
|
|
|
|
#file_name
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(expanded)
|
|
|
|
}
|
2019-11-22 11:47:35 -06:00
|
|
|
|
2019-11-24 18:01:51 -06:00
|
|
|
fn compile_error_expand(
|
|
|
|
_db: &dyn AstDatabase,
|
|
|
|
_id: MacroCallId,
|
|
|
|
tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
|
|
|
if tt.count() == 1 {
|
|
|
|
match &tt.token_trees[0] {
|
|
|
|
tt::TokenTree::Leaf(tt::Leaf::Literal(it)) => {
|
|
|
|
let s = it.text.as_str();
|
|
|
|
if s.contains(r#"""#) {
|
|
|
|
return Ok(quote! { loop { #it }});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(mbe::ExpandError::BindingError("Must be a string".into()))
|
|
|
|
}
|
|
|
|
|
2019-12-06 03:57:20 -06:00
|
|
|
fn format_args_expand(
|
|
|
|
_db: &dyn AstDatabase,
|
|
|
|
_id: MacroCallId,
|
|
|
|
_tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
|
|
|
// FIXME this is just a stub to make format macros type-check without mismatches
|
|
|
|
// We should make this at least insert the arguments, so that go to def etc. work within format macros
|
|
|
|
let expanded = quote! {
|
|
|
|
std::fmt::Arguments::new_v1(&[], &[])
|
|
|
|
};
|
|
|
|
Ok(expanded)
|
|
|
|
}
|
|
|
|
|
2019-11-22 11:47:35 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-12-05 08:10:33 -06:00
|
|
|
use crate::{test_db::TestDB, MacroCallKind, MacroCallLoc};
|
2019-11-22 11:47:35 -06:00
|
|
|
use ra_db::{fixture::WithFixture, SourceDatabase};
|
|
|
|
|
2019-11-23 07:54:39 -06:00
|
|
|
fn expand_builtin_macro(s: &str, expander: BuiltinFnLikeExpander) -> String {
|
2019-11-22 11:47:35 -06:00
|
|
|
let (db, file_id) = TestDB::with_single_file(&s);
|
|
|
|
let parsed = db.parse(file_id);
|
|
|
|
let macro_calls: Vec<_> =
|
|
|
|
parsed.syntax_node().descendants().filter_map(|it| ast::MacroCall::cast(it)).collect();
|
|
|
|
|
|
|
|
let ast_id_map = db.ast_id_map(file_id.into());
|
|
|
|
|
|
|
|
// the first one should be a macro_rules
|
|
|
|
let def = MacroDefId {
|
2019-12-05 08:10:33 -06:00
|
|
|
krate: Some(CrateId(0)),
|
|
|
|
ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(¯o_calls[0]))),
|
2019-11-22 11:47:35 -06:00
|
|
|
kind: MacroDefKind::BuiltIn(expander),
|
|
|
|
};
|
|
|
|
|
|
|
|
let loc = MacroCallLoc {
|
|
|
|
def,
|
2019-12-05 08:10:33 -06:00
|
|
|
kind: MacroCallKind::FnLike(AstId::new(
|
|
|
|
file_id.into(),
|
|
|
|
ast_id_map.ast_id(¯o_calls[1]),
|
|
|
|
)),
|
2019-11-22 11:47:35 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
let id = db.intern_macro(loc);
|
|
|
|
let parsed = db.parse_or_expand(id.as_file(MacroFileKind::Expr)).unwrap();
|
|
|
|
|
|
|
|
parsed.text().to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_column_expand() {
|
|
|
|
let expanded = expand_builtin_macro(
|
|
|
|
r#"
|
|
|
|
#[rustc_builtin_macro]
|
|
|
|
macro_rules! column {() => {}}
|
|
|
|
column!()
|
|
|
|
"#,
|
2019-11-23 07:54:39 -06:00
|
|
|
BuiltinFnLikeExpander::Column,
|
2019-11-22 11:47:35 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(expanded, "9");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_line_expand() {
|
|
|
|
let expanded = expand_builtin_macro(
|
|
|
|
r#"
|
|
|
|
#[rustc_builtin_macro]
|
|
|
|
macro_rules! line {() => {}}
|
|
|
|
line!()
|
|
|
|
"#,
|
2019-11-23 07:54:39 -06:00
|
|
|
BuiltinFnLikeExpander::Line,
|
2019-11-22 11:47:35 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(expanded, "4");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_stringify_expand() {
|
|
|
|
let expanded = expand_builtin_macro(
|
|
|
|
r#"
|
|
|
|
#[rustc_builtin_macro]
|
|
|
|
macro_rules! stringify {() => {}}
|
|
|
|
stringify!(a b c)
|
|
|
|
"#,
|
2019-11-23 07:54:39 -06:00
|
|
|
BuiltinFnLikeExpander::Stringify,
|
2019-11-22 11:47:35 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(expanded, "\"a b c\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_file_expand() {
|
|
|
|
let expanded = expand_builtin_macro(
|
|
|
|
r#"
|
|
|
|
#[rustc_builtin_macro]
|
|
|
|
macro_rules! file {() => {}}
|
|
|
|
file!()
|
|
|
|
"#,
|
2019-11-23 07:54:39 -06:00
|
|
|
BuiltinFnLikeExpander::File,
|
2019-11-22 11:47:35 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(expanded, "\"\"");
|
|
|
|
}
|
2019-11-24 18:01:51 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_compile_error_expand() {
|
|
|
|
let expanded = expand_builtin_macro(
|
|
|
|
r#"
|
|
|
|
#[rustc_builtin_macro]
|
|
|
|
macro_rules! compile_error {
|
|
|
|
($msg:expr) => ({ /* compiler built-in */ });
|
|
|
|
($msg:expr,) => ({ /* compiler built-in */ })
|
|
|
|
}
|
|
|
|
compile_error!("error!");
|
|
|
|
"#,
|
|
|
|
BuiltinFnLikeExpander::CompileError,
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(expanded, r#"loop{"error!"}"#);
|
|
|
|
}
|
2019-11-22 11:47:35 -06:00
|
|
|
}
|