rust/crates/ra_hir_expand/src/builtin_macro.rs

366 lines
9.8 KiB
Rust
Raw Normal View History

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},
name, AstId, CrateId, MacroCallId, MacroDefId, MacroDefKind, TextUnit,
2019-11-11 00:15:09 -06:00
};
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)
}
fn by_name(ident: &name::Name) -> Option<BuiltinFnLikeExpander> {
match ident {
$( id if id == &name::name![$name] => Some(BuiltinFnLikeExpander::$kind), )*
_ => return None,
}
}
2019-11-23 08:48:34 -06:00
}
pub fn find_builtin_macro(
ident: &name::Name,
krate: CrateId,
ast_id: AstId<ast::MacroCall>,
) -> Option<MacroDefId> {
let kind = BuiltinFnLikeExpander::by_name(ident)?;
2019-11-23 08:48:34 -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! {
2019-12-13 14:43:53 -06:00
(column, Column) => column_expand,
(compile_error, CompileError) => compile_error_expand,
(file, File) => file_expand,
(line, Line) => line_expand,
(stringify, Stringify) => stringify_expand,
(format_args, FormatArgs) => format_args_expand,
(env, Env) => env_expand,
(option_env, OptionEnv) => option_env_expand,
// format_args_nl only differs in that it adds a newline in the end,
// so we use the same stub expansion for now
2019-12-13 14:43:53 -06:00
(format_args_nl, FormatArgsNl) => format_args_expand
2019-11-22 11:47:35 -06:00
}
2019-11-11 00:15:09 -06:00
fn line_expand(
_db: &dyn AstDatabase,
_id: MacroCallId,
2019-11-11 00:15:09 -06:00
_tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
// dummy implementation for type-checking purposes
let line_num = 0;
2019-11-11 00:15:09 -06:00
let expanded = quote! {
#line_num
};
Ok(expanded)
}
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 = {
let arg = loc.kind.arg(db).ok_or_else(|| mbe::ExpandError::UnexpectedToken)?;
2019-12-20 08:43:01 -06:00
let macro_args = arg;
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
fn env_expand(
_db: &dyn AstDatabase,
_id: MacroCallId,
_tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
// dummy implementation for type-checking purposes
let expanded = quote! { "" };
Ok(expanded)
}
fn option_env_expand(
_db: &dyn AstDatabase,
_id: MacroCallId,
_tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
// dummy implementation for type-checking purposes
let expanded = quote! { std::option::Option::None::<&str> };
Ok(expanded)
}
2019-11-22 07:48:33 -06:00
fn column_expand(
_db: &dyn AstDatabase,
_id: MacroCallId,
2019-11-22 07:48:33 -06:00
_tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
// dummy implementation for type-checking purposes
let col_num = 0;
2019-11-22 07:48:33 -06:00
let expanded = quote! {
#col_num
};
Ok(expanded)
}
2019-11-22 09:05:04 -06:00
fn file_expand(
_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()))
}
fn format_args_expand(
_db: &dyn AstDatabase,
_id: MacroCallId,
2019-12-06 12:30:01 -06:00
tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
2019-12-08 02:26:17 -06:00
// We expand `format_args!("", a1, a2)` to
// ```
// std::fmt::Arguments::new_v1(&[], &[
// std::fmt::ArgumentV1::new(&arg1,std::fmt::Display::fmt),
// std::fmt::ArgumentV1::new(&arg2,std::fmt::Display::fmt),
// ])
// ```,
2019-12-06 12:30:01 -06:00
// which is still not really correct, but close enough for now
let mut args = Vec::new();
let mut current = Vec::new();
for tt in tt.token_trees.iter().cloned() {
match tt {
tt::TokenTree::Leaf(tt::Leaf::Punct(p)) if p.char == ',' => {
2019-12-08 02:26:17 -06:00
args.push(current);
2019-12-06 12:30:01 -06:00
current = Vec::new();
}
_ => {
current.push(tt);
}
}
}
if !current.is_empty() {
2019-12-08 02:26:17 -06:00
args.push(current);
2019-12-06 12:30:01 -06:00
}
if args.is_empty() {
return Err(mbe::ExpandError::NoMatchingRule);
}
let _format_string = args.remove(0);
2019-12-08 02:26:17 -06:00
let arg_tts = args.into_iter().flat_map(|arg| {
quote! { std::fmt::ArgumentV1::new(&(##arg), std::fmt::Display::fmt), }
}.token_trees).collect::<Vec<_>>();
let expanded = quote! {
2019-12-06 12:30:01 -06:00
std::fmt::Arguments::new_v1(&[], &[##arg_tts])
};
Ok(expanded)
}
2019-11-22 11:47:35 -06:00
#[cfg(test)]
mod tests {
use super::*;
use crate::{name::AsName, test_db::TestDB, AstNode, MacroCallKind, MacroCallLoc};
2019-11-22 11:47:35 -06:00
use ra_db::{fixture::WithFixture, SourceDatabase};
use ra_syntax::ast::NameOwner;
2019-11-22 11:47:35 -06:00
fn expand_builtin_macro(s: &str) -> 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());
let expander =
BuiltinFnLikeExpander::by_name(&macro_calls[0].name().unwrap().as_name()).unwrap();
2019-11-22 11:47:35 -06:00
// the first one should be a macro_rules
let def = MacroDefId {
krate: Some(CrateId(0)),
ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(&macro_calls[0]))),
2019-11-22 11:47:35 -06:00
kind: MacroDefKind::BuiltIn(expander),
};
let loc = MacroCallLoc {
def,
kind: MacroCallKind::FnLike(AstId::new(
file_id.into(),
ast_id_map.ast_id(&macro_calls[1]),
)),
2019-11-22 11:47:35 -06:00
};
let id = db.intern_macro(loc);
2019-12-08 02:16:52 -06:00
let parsed = db.parse_or_expand(id.as_file()).unwrap();
2019-11-22 11:47:35 -06:00
parsed.text().to_string()
}
#[test]
fn test_column_expand() {
let expanded = expand_builtin_macro(
r#"
#[rustc_builtin_macro]
macro_rules! column {() => {}}
column!()
"#,
2019-11-22 11:47:35 -06:00
);
assert_eq!(expanded, "0");
2019-11-22 11:47:35 -06:00
}
#[test]
fn test_line_expand() {
let expanded = expand_builtin_macro(
r#"
#[rustc_builtin_macro]
macro_rules! line {() => {}}
line!()
"#,
2019-11-22 11:47:35 -06:00
);
assert_eq!(expanded, "0");
2019-11-22 11:47:35 -06:00
}
#[test]
fn test_stringify_expand() {
let expanded = expand_builtin_macro(
r#"
#[rustc_builtin_macro]
macro_rules! stringify {() => {}}
stringify!(a b c)
"#,
2019-11-22 11:47:35 -06:00
);
assert_eq!(expanded, "\"a b c\"");
}
#[test]
fn test_env_expand() {
let expanded = expand_builtin_macro(
r#"
#[rustc_builtin_macro]
macro_rules! env {() => {}}
env!("TEST_ENV_VAR")
"#,
);
assert_eq!(expanded, "\"\"");
}
#[test]
fn test_option_env_expand() {
let expanded = expand_builtin_macro(
r#"
#[rustc_builtin_macro]
macro_rules! option_env {() => {}}
option_env!("TEST_ENV_VAR")
"#,
);
assert_eq!(expanded, "std::option::Option::None:: <&str>");
}
2019-11-22 11:47:35 -06:00
#[test]
fn test_file_expand() {
let expanded = expand_builtin_macro(
r#"
#[rustc_builtin_macro]
macro_rules! file {() => {}}
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!");
"#,
2019-11-24 18:01:51 -06:00
);
assert_eq!(expanded, r#"loop{"error!"}"#);
}
2019-12-06 12:30:01 -06:00
#[test]
fn test_format_args_expand() {
let expanded = expand_builtin_macro(
r#"
#[rustc_builtin_macro]
macro_rules! format_args {
($fmt:expr) => ({ /* compiler built-in */ });
($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ })
}
format_args!("{} {:?}", arg1(a, b, c), arg2);
"#,
2019-12-06 12:30:01 -06:00
);
2019-12-12 07:34:03 -06:00
assert_eq!(
expanded,
r#"std::fmt::Arguments::new_v1(&[] ,&[std::fmt::ArgumentV1::new(&(arg1(a,b,c)),std::fmt::Display::fmt),std::fmt::ArgumentV1::new(&(arg2),std::fmt::Display::fmt),])"#
);
2019-12-06 12:30:01 -06:00
}
2019-11-22 11:47:35 -06:00
}