2019-11-09 21:03:24 -06:00
|
|
|
//! Builtin macro
|
2019-11-11 00:15:09 -06:00
|
|
|
use crate::{
|
2020-06-11 05:08:24 -05:00
|
|
|
db::AstDatabase, name, quote, AstId, CrateId, EagerMacroId, LazyMacroId, MacroCallId,
|
|
|
|
MacroDefId, MacroDefKind, TextSize,
|
2019-11-11 00:15:09 -06:00
|
|
|
};
|
|
|
|
|
2020-12-09 10:01:15 -06:00
|
|
|
use base_db::{AnchoredPath, FileId};
|
2020-03-02 00:05:15 -06:00
|
|
|
use either::Either;
|
2020-11-30 12:21:25 -06:00
|
|
|
use mbe::{parse_to_token_tree, ExpandResult};
|
2020-08-12 10:06:49 -05:00
|
|
|
use parser::FragmentKind;
|
2020-11-06 15:30:58 -06:00
|
|
|
use syntax::ast::{self, AstToken};
|
2019-11-09 21:03:24 -06:00
|
|
|
|
2019-11-22 11:47:35 -06:00
|
|
|
macro_rules! register_builtin {
|
2020-03-02 00:05:15 -06:00
|
|
|
( LAZY: $(($name:ident, $kind: ident) => $expand:ident),* , EAGER: $(($e_name:ident, $e_kind: ident) => $e_expand:ident),* ) => {
|
2019-11-23 08:48:34 -06:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub enum BuiltinFnLikeExpander {
|
|
|
|
$($kind),*
|
|
|
|
}
|
|
|
|
|
2020-03-02 00:05:15 -06:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub enum EagerExpander {
|
|
|
|
$($e_kind),*
|
|
|
|
}
|
|
|
|
|
2019-11-23 08:48:34 -06:00
|
|
|
impl BuiltinFnLikeExpander {
|
|
|
|
pub fn expand(
|
|
|
|
&self,
|
|
|
|
db: &dyn AstDatabase,
|
2020-03-02 00:05:15 -06:00
|
|
|
id: LazyMacroId,
|
2019-11-23 08:48:34 -06:00
|
|
|
tt: &tt::Subtree,
|
2020-11-30 12:21:25 -06:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2019-11-23 08:48:34 -06:00
|
|
|
let expander = match *self {
|
|
|
|
$( BuiltinFnLikeExpander::$kind => $expand, )*
|
|
|
|
};
|
|
|
|
expander(db, id, tt)
|
|
|
|
}
|
2020-03-02 00:05:15 -06:00
|
|
|
}
|
2019-12-21 06:33:44 -06:00
|
|
|
|
2020-03-02 00:05:15 -06:00
|
|
|
impl EagerExpander {
|
|
|
|
pub fn expand(
|
|
|
|
&self,
|
2020-03-06 08:58:45 -06:00
|
|
|
db: &dyn AstDatabase,
|
|
|
|
arg_id: EagerMacroId,
|
2020-03-02 00:05:15 -06:00
|
|
|
tt: &tt::Subtree,
|
2020-11-30 12:21:25 -06:00
|
|
|
) -> ExpandResult<Option<(tt::Subtree, FragmentKind)>> {
|
2020-03-02 00:05:15 -06:00
|
|
|
let expander = match *self {
|
|
|
|
$( EagerExpander::$e_kind => $e_expand, )*
|
|
|
|
};
|
2020-03-06 08:58:45 -06:00
|
|
|
expander(db,arg_id,tt)
|
2019-12-21 06:33:44 -06:00
|
|
|
}
|
2019-11-23 08:48:34 -06:00
|
|
|
}
|
|
|
|
|
2020-03-02 00:05:15 -06:00
|
|
|
fn find_by_name(ident: &name::Name) -> Option<Either<BuiltinFnLikeExpander, EagerExpander>> {
|
|
|
|
match ident {
|
|
|
|
$( id if id == &name::name![$name] => Some(Either::Left(BuiltinFnLikeExpander::$kind)), )*
|
|
|
|
$( id if id == &name::name![$e_name] => Some(Either::Right(EagerExpander::$e_kind)), )*
|
|
|
|
_ => return None,
|
|
|
|
}
|
2019-11-23 08:48:34 -06:00
|
|
|
}
|
2019-11-22 11:47:35 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-02 00:05:15 -06:00
|
|
|
pub fn find_builtin_macro(
|
|
|
|
ident: &name::Name,
|
|
|
|
krate: CrateId,
|
2020-12-15 11:43:19 -06:00
|
|
|
ast_id: AstId<ast::Macro>,
|
2020-03-02 00:05:15 -06:00
|
|
|
) -> Option<MacroDefId> {
|
|
|
|
let kind = find_by_name(ident)?;
|
|
|
|
|
|
|
|
match kind {
|
|
|
|
Either::Left(kind) => Some(MacroDefId {
|
2020-12-15 13:33:05 -06:00
|
|
|
krate,
|
2020-03-02 00:05:15 -06:00
|
|
|
ast_id: Some(ast_id),
|
|
|
|
kind: MacroDefKind::BuiltIn(kind),
|
2020-04-30 22:23:03 -05:00
|
|
|
local_inner: false,
|
2020-03-02 00:05:15 -06:00
|
|
|
}),
|
|
|
|
Either::Right(kind) => Some(MacroDefId {
|
2020-12-15 13:33:05 -06:00
|
|
|
krate,
|
2020-03-02 00:05:15 -06:00
|
|
|
ast_id: Some(ast_id),
|
|
|
|
kind: MacroDefKind::BuiltInEager(kind),
|
2020-04-30 22:23:03 -05:00
|
|
|
local_inner: false,
|
2020-03-02 00:05:15 -06:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 11:47:35 -06:00
|
|
|
register_builtin! {
|
2020-03-02 00:05:15 -06:00
|
|
|
LAZY:
|
2019-12-13 14:43:53 -06:00
|
|
|
(column, Column) => column_expand,
|
|
|
|
(file, File) => file_expand,
|
|
|
|
(line, Line) => line_expand,
|
2020-12-14 09:38:53 -06:00
|
|
|
(module_path, ModulePath) => module_path_expand,
|
2020-03-11 10:08:12 -05:00
|
|
|
(assert, Assert) => assert_expand,
|
2019-12-13 14:43:53 -06:00
|
|
|
(stringify, Stringify) => stringify_expand,
|
|
|
|
(format_args, FormatArgs) => format_args_expand,
|
2019-12-06 03:57:20 -06:00
|
|
|
// format_args_nl only differs in that it adds a newline in the end,
|
|
|
|
// so we use the same stub expansion for now
|
2020-03-02 00:05:15 -06:00
|
|
|
(format_args_nl, FormatArgsNl) => format_args_expand,
|
2020-12-15 03:05:20 -06:00
|
|
|
(llvm_asm, LlvmAsm) => asm_expand,
|
|
|
|
(asm, Asm) => asm_expand,
|
2020-03-02 00:05:15 -06:00
|
|
|
|
|
|
|
EAGER:
|
2020-12-03 08:31:04 -06:00
|
|
|
(compile_error, CompileError) => compile_error_expand,
|
2020-03-06 08:58:45 -06:00
|
|
|
(concat, Concat) => concat_expand,
|
2020-03-10 09:01:08 -05:00
|
|
|
(include, Include) => include_expand,
|
2020-06-27 13:02:47 -05:00
|
|
|
(include_bytes, IncludeBytes) => include_bytes_expand,
|
2020-06-27 07:31:19 -05:00
|
|
|
(include_str, IncludeStr) => include_str_expand,
|
2020-03-10 09:01:08 -05:00
|
|
|
(env, Env) => env_expand,
|
|
|
|
(option_env, OptionEnv) => option_env_expand
|
2019-11-22 11:47:35 -06:00
|
|
|
}
|
|
|
|
|
2020-12-14 09:38:53 -06:00
|
|
|
fn module_path_expand(
|
|
|
|
_db: &dyn AstDatabase,
|
|
|
|
_id: LazyMacroId,
|
|
|
|
_tt: &tt::Subtree,
|
|
|
|
) -> ExpandResult<tt::Subtree> {
|
|
|
|
// Just return a dummy result.
|
|
|
|
ExpandResult::ok(quote! { "module::path" })
|
|
|
|
}
|
|
|
|
|
2019-11-11 00:15:09 -06:00
|
|
|
fn line_expand(
|
2020-01-14 11:47:01 -06:00
|
|
|
_db: &dyn AstDatabase,
|
2020-03-02 00:05:15 -06:00
|
|
|
_id: LazyMacroId,
|
2019-11-11 00:15:09 -06:00
|
|
|
_tt: &tt::Subtree,
|
2020-11-30 12:21:25 -06:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2020-01-14 11:47:01 -06:00
|
|
|
// dummy implementation for type-checking purposes
|
|
|
|
let line_num = 0;
|
2019-11-11 00:15:09 -06:00
|
|
|
let expanded = quote! {
|
|
|
|
#line_num
|
|
|
|
};
|
|
|
|
|
2020-11-30 12:21:25 -06:00
|
|
|
ExpandResult::ok(expanded)
|
2019-11-11 00:15:09 -06:00
|
|
|
}
|
2019-11-22 00:56:50 -06:00
|
|
|
|
|
|
|
fn stringify_expand(
|
|
|
|
db: &dyn AstDatabase,
|
2020-03-02 00:05:15 -06:00
|
|
|
id: LazyMacroId,
|
2019-11-22 00:56:50 -06:00
|
|
|
_tt: &tt::Subtree,
|
2020-11-30 12:21:25 -06:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2019-11-22 00:56:50 -06:00
|
|
|
let loc = db.lookup_intern_macro(id);
|
|
|
|
|
|
|
|
let macro_content = {
|
2020-11-30 12:21:25 -06:00
|
|
|
let arg = match loc.kind.arg(db) {
|
|
|
|
Some(arg) => arg,
|
|
|
|
None => return ExpandResult::only_err(mbe::ExpandError::UnexpectedToken),
|
|
|
|
};
|
2019-12-20 08:43:01 -06:00
|
|
|
let macro_args = arg;
|
2019-11-22 00:56:50 -06:00
|
|
|
let text = macro_args.text();
|
2020-04-24 16:40:41 -05:00
|
|
|
let without_parens = TextSize::of('(')..text.len() - TextSize::of(')');
|
2019-11-22 00:56:50 -06:00
|
|
|
text.slice(without_parens).to_string()
|
|
|
|
};
|
|
|
|
|
|
|
|
let expanded = quote! {
|
|
|
|
#macro_content
|
|
|
|
};
|
|
|
|
|
2020-11-30 12:21:25 -06:00
|
|
|
ExpandResult::ok(expanded)
|
2019-11-22 00:56:50 -06:00
|
|
|
}
|
2019-11-22 09:05:04 -06:00
|
|
|
|
2019-11-22 07:48:33 -06:00
|
|
|
fn column_expand(
|
2020-01-14 11:47:01 -06:00
|
|
|
_db: &dyn AstDatabase,
|
2020-03-02 00:05:15 -06:00
|
|
|
_id: LazyMacroId,
|
2019-11-22 07:48:33 -06:00
|
|
|
_tt: &tt::Subtree,
|
2020-11-30 12:21:25 -06:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2020-01-14 11:47:01 -06:00
|
|
|
// dummy implementation for type-checking purposes
|
|
|
|
let col_num = 0;
|
2019-11-22 07:48:33 -06:00
|
|
|
let expanded = quote! {
|
|
|
|
#col_num
|
|
|
|
};
|
|
|
|
|
2020-11-30 12:21:25 -06:00
|
|
|
ExpandResult::ok(expanded)
|
2019-11-22 07:48:33 -06:00
|
|
|
}
|
|
|
|
|
2020-03-11 10:08:12 -05:00
|
|
|
fn assert_expand(
|
|
|
|
_db: &dyn AstDatabase,
|
|
|
|
_id: LazyMacroId,
|
|
|
|
tt: &tt::Subtree,
|
2020-11-30 12:21:25 -06:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2020-03-11 10:08:12 -05:00
|
|
|
// A hacky implementation for goto def and hover
|
2020-03-11 12:57:34 -05:00
|
|
|
// We expand `assert!(cond, arg1, arg2)` to
|
2020-03-11 10:08:12 -05:00
|
|
|
// ```
|
2020-03-11 12:57:34 -05:00
|
|
|
// {(cond, &(arg1), &(arg2));}
|
2020-03-11 10:08:12 -05:00
|
|
|
// ```,
|
|
|
|
// which is wrong but useful.
|
|
|
|
|
|
|
|
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 == ',' => {
|
|
|
|
args.push(current);
|
|
|
|
current = Vec::new();
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
current.push(tt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !current.is_empty() {
|
|
|
|
args.push(current);
|
|
|
|
}
|
|
|
|
|
|
|
|
let arg_tts = args.into_iter().flat_map(|arg| {
|
|
|
|
quote! { &(##arg), }
|
|
|
|
}.token_trees).collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let expanded = quote! {
|
|
|
|
{ { (##arg_tts); } }
|
|
|
|
};
|
2020-11-30 12:21:25 -06:00
|
|
|
ExpandResult::ok(expanded)
|
2020-03-11 10:08:12 -05:00
|
|
|
}
|
|
|
|
|
2019-11-22 09:05:04 -06:00
|
|
|
fn file_expand(
|
2019-12-05 08:10:33 -06:00
|
|
|
_db: &dyn AstDatabase,
|
2020-03-02 00:05:15 -06:00
|
|
|
_id: LazyMacroId,
|
2019-11-22 09:05:04 -06:00
|
|
|
_tt: &tt::Subtree,
|
2020-11-30 12:21:25 -06:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2019-11-22 09:05:04 -06:00
|
|
|
// FIXME: RA purposefully lacks knowledge of absolute file names
|
|
|
|
// so just return "".
|
|
|
|
let file_name = "";
|
|
|
|
|
|
|
|
let expanded = quote! {
|
|
|
|
#file_name
|
|
|
|
};
|
|
|
|
|
2020-11-30 12:21:25 -06:00
|
|
|
ExpandResult::ok(expanded)
|
2019-11-22 09:05:04 -06:00
|
|
|
}
|
2019-11-22 11:47:35 -06:00
|
|
|
|
2019-12-06 03:57:20 -06:00
|
|
|
fn format_args_expand(
|
|
|
|
_db: &dyn AstDatabase,
|
2020-03-02 00:05:15 -06:00
|
|
|
_id: LazyMacroId,
|
2019-12-06 12:30:01 -06:00
|
|
|
tt: &tt::Subtree,
|
2020-11-30 12:21:25 -06:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
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() {
|
2020-11-30 12:21:25 -06:00
|
|
|
return ExpandResult::only_err(mbe::ExpandError::NoMatchingRule);
|
2019-12-06 12:30:01 -06:00
|
|
|
}
|
2020-12-10 06:46:47 -06:00
|
|
|
for arg in &mut args {
|
|
|
|
// Remove `key =`.
|
|
|
|
if matches!(arg.get(1), Some(tt::TokenTree::Leaf(tt::Leaf::Punct(p))) if p.char == '=') {
|
|
|
|
arg.drain(..2);
|
|
|
|
}
|
|
|
|
}
|
2019-12-06 12:30:01 -06:00
|
|
|
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<_>>();
|
2019-12-06 03:57:20 -06:00
|
|
|
let expanded = quote! {
|
2019-12-06 12:30:01 -06:00
|
|
|
std::fmt::Arguments::new_v1(&[], &[##arg_tts])
|
2019-12-06 03:57:20 -06:00
|
|
|
};
|
2020-11-30 12:21:25 -06:00
|
|
|
ExpandResult::ok(expanded)
|
2019-12-06 03:57:20 -06:00
|
|
|
}
|
|
|
|
|
2020-12-15 03:05:20 -06:00
|
|
|
fn asm_expand(
|
|
|
|
_db: &dyn AstDatabase,
|
|
|
|
_id: LazyMacroId,
|
|
|
|
_tt: &tt::Subtree,
|
|
|
|
) -> ExpandResult<tt::Subtree> {
|
|
|
|
// both asm and llvm_asm don't return anything, so we can expand them to nothing,
|
|
|
|
// for now
|
|
|
|
let expanded = quote! {
|
|
|
|
()
|
|
|
|
};
|
|
|
|
ExpandResult::ok(expanded)
|
|
|
|
}
|
|
|
|
|
2020-03-02 00:05:15 -06:00
|
|
|
fn unquote_str(lit: &tt::Literal) -> Option<String> {
|
|
|
|
let lit = ast::make::tokens::literal(&lit.to_string());
|
|
|
|
let token = ast::String::cast(lit)?;
|
2020-07-09 12:21:41 -05:00
|
|
|
token.value().map(|it| it.into_owned())
|
2020-03-02 00:05:15 -06:00
|
|
|
}
|
|
|
|
|
2020-12-03 08:31:04 -06:00
|
|
|
fn compile_error_expand(
|
|
|
|
_db: &dyn AstDatabase,
|
|
|
|
_id: EagerMacroId,
|
|
|
|
tt: &tt::Subtree,
|
|
|
|
) -> ExpandResult<Option<(tt::Subtree, FragmentKind)>> {
|
|
|
|
let err = match &*tt.token_trees {
|
|
|
|
[tt::TokenTree::Leaf(tt::Leaf::Literal(it))] => {
|
|
|
|
let text = it.text.as_str();
|
|
|
|
if text.starts_with('"') && text.ends_with('"') {
|
|
|
|
// FIXME: does not handle raw strings
|
2020-12-03 12:07:37 -06:00
|
|
|
mbe::ExpandError::Other(text[1..text.len() - 1].to_string())
|
2020-12-03 08:31:04 -06:00
|
|
|
} else {
|
|
|
|
mbe::ExpandError::BindingError("`compile_error!` argument must be a string".into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => mbe::ExpandError::BindingError("`compile_error!` argument must be a string".into()),
|
|
|
|
};
|
|
|
|
|
|
|
|
ExpandResult { value: Some((quote! {}, FragmentKind::Items)), err: Some(err) }
|
|
|
|
}
|
|
|
|
|
2020-03-06 08:58:45 -06:00
|
|
|
fn concat_expand(
|
|
|
|
_db: &dyn AstDatabase,
|
|
|
|
_arg_id: EagerMacroId,
|
|
|
|
tt: &tt::Subtree,
|
2020-11-30 12:21:25 -06:00
|
|
|
) -> ExpandResult<Option<(tt::Subtree, FragmentKind)>> {
|
2020-12-08 13:06:41 -06:00
|
|
|
let mut err = None;
|
2020-03-02 00:05:15 -06:00
|
|
|
let mut text = String::new();
|
|
|
|
for (i, t) in tt.token_trees.iter().enumerate() {
|
|
|
|
match t {
|
|
|
|
tt::TokenTree::Leaf(tt::Leaf::Literal(it)) if i % 2 == 0 => {
|
2020-12-08 13:06:41 -06:00
|
|
|
// concat works with string and char literals, so remove any quotes.
|
|
|
|
// It also works with integer, float and boolean literals, so just use the rest
|
|
|
|
// as-is.
|
|
|
|
|
|
|
|
text += it
|
|
|
|
.text
|
|
|
|
.trim_start_matches(|c| match c {
|
|
|
|
'r' | '#' | '\'' | '"' => true,
|
|
|
|
_ => false,
|
|
|
|
})
|
|
|
|
.trim_end_matches(|c| match c {
|
|
|
|
'#' | '\'' | '"' => true,
|
|
|
|
_ => false,
|
|
|
|
});
|
2020-03-02 00:05:15 -06:00
|
|
|
}
|
|
|
|
tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) if i % 2 == 1 && punct.char == ',' => (),
|
2020-12-08 13:06:41 -06:00
|
|
|
_ => {
|
|
|
|
err.get_or_insert(mbe::ExpandError::UnexpectedToken);
|
|
|
|
}
|
2020-03-02 00:05:15 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 13:06:41 -06:00
|
|
|
ExpandResult { value: Some((quote!(#text), FragmentKind::Expr)), err }
|
2020-03-02 00:05:15 -06:00
|
|
|
}
|
|
|
|
|
2020-06-27 07:31:19 -05:00
|
|
|
fn relative_file(
|
|
|
|
db: &dyn AstDatabase,
|
|
|
|
call_id: MacroCallId,
|
|
|
|
path: &str,
|
|
|
|
allow_recursion: bool,
|
|
|
|
) -> Option<FileId> {
|
2020-03-06 08:58:45 -06:00
|
|
|
let call_site = call_id.as_file().original_file(db);
|
2020-12-09 10:01:15 -06:00
|
|
|
let path = AnchoredPath { anchor: call_site, path };
|
|
|
|
let res = db.resolve_path(path)?;
|
2020-06-05 09:45:20 -05:00
|
|
|
// Prevent include itself
|
2020-06-27 07:31:19 -05:00
|
|
|
if res == call_site && !allow_recursion {
|
2020-06-05 09:45:20 -05:00
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(res)
|
2020-03-07 02:25:43 -06:00
|
|
|
}
|
2020-03-06 08:58:45 -06:00
|
|
|
}
|
|
|
|
|
2020-03-10 09:01:08 -05:00
|
|
|
fn parse_string(tt: &tt::Subtree) -> Result<String, mbe::ExpandError> {
|
|
|
|
tt.token_trees
|
2020-03-06 08:58:45 -06:00
|
|
|
.get(0)
|
|
|
|
.and_then(|tt| match tt {
|
|
|
|
tt::TokenTree::Leaf(tt::Leaf::Literal(it)) => unquote_str(&it),
|
|
|
|
_ => None,
|
|
|
|
})
|
2020-03-10 09:01:08 -05:00
|
|
|
.ok_or_else(|| mbe::ExpandError::ConversionError)
|
|
|
|
}
|
2020-03-06 08:58:45 -06:00
|
|
|
|
2020-03-10 09:01:08 -05:00
|
|
|
fn include_expand(
|
|
|
|
db: &dyn AstDatabase,
|
|
|
|
arg_id: EagerMacroId,
|
|
|
|
tt: &tt::Subtree,
|
2020-11-30 12:21:25 -06:00
|
|
|
) -> ExpandResult<Option<(tt::Subtree, FragmentKind)>> {
|
|
|
|
let res = (|| {
|
|
|
|
let path = parse_string(tt)?;
|
|
|
|
let file_id = relative_file(db, arg_id.into(), &path, false)
|
|
|
|
.ok_or_else(|| mbe::ExpandError::ConversionError)?;
|
|
|
|
|
|
|
|
Ok(parse_to_token_tree(&db.file_text(file_id))
|
|
|
|
.ok_or_else(|| mbe::ExpandError::ConversionError)?
|
|
|
|
.0)
|
|
|
|
})();
|
|
|
|
|
|
|
|
match res {
|
|
|
|
Ok(res) => {
|
|
|
|
// FIXME:
|
|
|
|
// Handle include as expression
|
|
|
|
ExpandResult::ok(Some((res, FragmentKind::Items)))
|
|
|
|
}
|
|
|
|
Err(e) => ExpandResult::only_err(e),
|
|
|
|
}
|
2020-03-06 08:58:45 -06:00
|
|
|
}
|
|
|
|
|
2020-06-27 13:02:47 -05:00
|
|
|
fn include_bytes_expand(
|
|
|
|
_db: &dyn AstDatabase,
|
|
|
|
_arg_id: EagerMacroId,
|
|
|
|
tt: &tt::Subtree,
|
2020-11-30 12:21:25 -06:00
|
|
|
) -> ExpandResult<Option<(tt::Subtree, FragmentKind)>> {
|
|
|
|
if let Err(e) = parse_string(tt) {
|
|
|
|
return ExpandResult::only_err(e);
|
|
|
|
}
|
2020-06-27 13:02:47 -05:00
|
|
|
|
|
|
|
// FIXME: actually read the file here if the user asked for macro expansion
|
|
|
|
let res = tt::Subtree {
|
|
|
|
delimiter: None,
|
|
|
|
token_trees: vec![tt::TokenTree::Leaf(tt::Leaf::Literal(tt::Literal {
|
|
|
|
text: r#"b"""#.into(),
|
|
|
|
id: tt::TokenId::unspecified(),
|
|
|
|
}))],
|
|
|
|
};
|
2020-11-30 12:21:25 -06:00
|
|
|
ExpandResult::ok(Some((res, FragmentKind::Expr)))
|
2020-06-27 13:02:47 -05:00
|
|
|
}
|
|
|
|
|
2020-06-27 07:31:19 -05:00
|
|
|
fn include_str_expand(
|
|
|
|
db: &dyn AstDatabase,
|
|
|
|
arg_id: EagerMacroId,
|
|
|
|
tt: &tt::Subtree,
|
2020-11-30 12:21:25 -06:00
|
|
|
) -> ExpandResult<Option<(tt::Subtree, FragmentKind)>> {
|
|
|
|
let path = match parse_string(tt) {
|
|
|
|
Ok(it) => it,
|
|
|
|
Err(e) => return ExpandResult::only_err(e),
|
|
|
|
};
|
2020-06-27 07:31:19 -05:00
|
|
|
|
|
|
|
// FIXME: we're not able to read excluded files (which is most of them because
|
|
|
|
// it's unusual to `include_str!` a Rust file), but we can return an empty string.
|
|
|
|
// Ideally, we'd be able to offer a precise expansion if the user asks for macro
|
|
|
|
// expansion.
|
|
|
|
let file_id = match relative_file(db, arg_id.into(), &path, true) {
|
|
|
|
Some(file_id) => file_id,
|
|
|
|
None => {
|
2020-11-30 12:21:25 -06:00
|
|
|
return ExpandResult::ok(Some((quote!(""), FragmentKind::Expr)));
|
2020-06-27 07:31:19 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let text = db.file_text(file_id);
|
|
|
|
let text = &*text;
|
|
|
|
|
2020-11-30 12:21:25 -06:00
|
|
|
ExpandResult::ok(Some((quote!(#text), FragmentKind::Expr)))
|
2020-06-27 07:31:19 -05:00
|
|
|
}
|
|
|
|
|
2020-03-10 09:01:08 -05:00
|
|
|
fn get_env_inner(db: &dyn AstDatabase, arg_id: EagerMacroId, key: &str) -> Option<String> {
|
2020-06-11 05:08:24 -05:00
|
|
|
let krate = db.lookup_intern_eager_expansion(arg_id).krate;
|
2020-03-10 09:01:08 -05:00
|
|
|
db.crate_graph()[krate].env.get(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn env_expand(
|
|
|
|
db: &dyn AstDatabase,
|
|
|
|
arg_id: EagerMacroId,
|
|
|
|
tt: &tt::Subtree,
|
2020-11-30 12:21:25 -06:00
|
|
|
) -> ExpandResult<Option<(tt::Subtree, FragmentKind)>> {
|
|
|
|
let key = match parse_string(tt) {
|
|
|
|
Ok(it) => it,
|
|
|
|
Err(e) => return ExpandResult::only_err(e),
|
|
|
|
};
|
2020-03-10 09:01:08 -05:00
|
|
|
|
2020-12-02 10:03:18 -06:00
|
|
|
let mut err = None;
|
|
|
|
let s = get_env_inner(db, arg_id, &key).unwrap_or_else(|| {
|
|
|
|
// The only variable rust-analyzer ever sets is `OUT_DIR`, so only diagnose that to avoid
|
|
|
|
// unnecessary diagnostics for eg. `CARGO_PKG_NAME`.
|
|
|
|
if key == "OUT_DIR" {
|
|
|
|
err = Some(mbe::ExpandError::Other(
|
|
|
|
r#"`OUT_DIR` not set, enable "load out dirs from check" to fix"#.into(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the variable is unset, still return a dummy string to help type inference along.
|
|
|
|
// We cannot use an empty string here, because for
|
|
|
|
// `include!(concat!(env!("OUT_DIR"), "/foo.rs"))` will become
|
|
|
|
// `include!("foo.rs"), which might go to infinite loop
|
|
|
|
"__RA_UNIMPLEMENTED__".to_string()
|
|
|
|
});
|
2020-03-10 09:01:08 -05:00
|
|
|
let expanded = quote! { #s };
|
|
|
|
|
2020-12-02 10:03:18 -06:00
|
|
|
ExpandResult { value: Some((expanded, FragmentKind::Expr)), err }
|
2020-03-10 09:01:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn option_env_expand(
|
|
|
|
db: &dyn AstDatabase,
|
|
|
|
arg_id: EagerMacroId,
|
|
|
|
tt: &tt::Subtree,
|
2020-11-30 12:21:25 -06:00
|
|
|
) -> ExpandResult<Option<(tt::Subtree, FragmentKind)>> {
|
|
|
|
let key = match parse_string(tt) {
|
|
|
|
Ok(it) => it,
|
|
|
|
Err(e) => return ExpandResult::only_err(e),
|
|
|
|
};
|
|
|
|
|
2020-03-10 09:01:08 -05:00
|
|
|
let expanded = match get_env_inner(db, arg_id, &key) {
|
|
|
|
None => quote! { std::option::Option::None::<&str> },
|
|
|
|
Some(s) => quote! { std::option::Some(#s) },
|
|
|
|
};
|
|
|
|
|
2020-11-30 12:21:25 -06:00
|
|
|
ExpandResult::ok(Some((expanded, FragmentKind::Expr)))
|
2020-03-10 09:01:08 -05:00
|
|
|
}
|
|
|
|
|
2019-11-22 11:47:35 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-03-10 09:01:08 -05:00
|
|
|
use crate::{
|
|
|
|
name::AsName, test_db::TestDB, AstNode, EagerCallLoc, MacroCallId, MacroCallKind,
|
|
|
|
MacroCallLoc,
|
|
|
|
};
|
2020-08-13 09:25:38 -05:00
|
|
|
use base_db::{fixture::WithFixture, SourceDatabase};
|
2020-03-10 09:01:08 -05:00
|
|
|
use std::sync::Arc;
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::ast::NameOwner;
|
2019-11-22 11:47:35 -06:00
|
|
|
|
2020-03-02 00:05:15 -06:00
|
|
|
fn expand_builtin_macro(ra_fixture: &str) -> String {
|
|
|
|
let (db, file_id) = TestDB::with_single_file(&ra_fixture);
|
2019-11-22 11:47:35 -06:00
|
|
|
let parsed = db.parse(file_id);
|
2020-12-15 11:43:19 -06:00
|
|
|
let mut macro_rules: Vec<_> =
|
2020-12-15 08:37:37 -06:00
|
|
|
parsed.syntax_node().descendants().filter_map(ast::MacroRules::cast).collect();
|
2020-12-15 11:43:19 -06:00
|
|
|
let mut macro_calls: Vec<_> =
|
2020-02-18 06:53:02 -06:00
|
|
|
parsed.syntax_node().descendants().filter_map(ast::MacroCall::cast).collect();
|
2019-11-22 11:47:35 -06:00
|
|
|
|
|
|
|
let ast_id_map = db.ast_id_map(file_id.into());
|
|
|
|
|
2020-12-15 08:37:37 -06:00
|
|
|
assert_eq!(macro_rules.len(), 1, "test must contain exactly 1 `macro_rules!`");
|
|
|
|
assert_eq!(macro_calls.len(), 1, "test must contain exactly 1 macro call");
|
2020-12-15 11:43:19 -06:00
|
|
|
let macro_rules = ast::Macro::from(macro_rules.pop().unwrap());
|
|
|
|
let macro_call = macro_calls.pop().unwrap();
|
|
|
|
|
|
|
|
let expander = find_by_name(¯o_rules.name().unwrap().as_name()).unwrap();
|
2019-12-21 06:33:44 -06:00
|
|
|
|
2020-06-11 05:08:24 -05:00
|
|
|
let krate = CrateId(0);
|
2020-03-10 09:01:08 -05:00
|
|
|
let file_id = match expander {
|
|
|
|
Either::Left(expander) => {
|
|
|
|
// the first one should be a macro_rules
|
|
|
|
let def = MacroDefId {
|
2020-12-15 13:33:05 -06:00
|
|
|
krate: CrateId(0),
|
2020-12-15 11:43:19 -06:00
|
|
|
ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(¯o_rules))),
|
2020-03-10 09:01:08 -05:00
|
|
|
kind: MacroDefKind::BuiltIn(expander),
|
2020-04-30 22:23:03 -05:00
|
|
|
local_inner: false,
|
2020-03-10 09:01:08 -05:00
|
|
|
};
|
2019-11-22 11:47:35 -06:00
|
|
|
|
2020-03-10 09:01:08 -05:00
|
|
|
let loc = MacroCallLoc {
|
|
|
|
def,
|
2020-06-11 05:08:24 -05:00
|
|
|
krate,
|
2020-03-10 09:01:08 -05:00
|
|
|
kind: MacroCallKind::FnLike(AstId::new(
|
|
|
|
file_id.into(),
|
2020-12-15 11:43:19 -06:00
|
|
|
ast_id_map.ast_id(¯o_call),
|
2020-03-10 09:01:08 -05:00
|
|
|
)),
|
|
|
|
};
|
|
|
|
|
|
|
|
let id: MacroCallId = db.intern_macro(loc).into();
|
|
|
|
id.as_file()
|
|
|
|
}
|
|
|
|
Either::Right(expander) => {
|
|
|
|
// the first one should be a macro_rules
|
|
|
|
let def = MacroDefId {
|
2020-12-15 13:33:05 -06:00
|
|
|
krate,
|
2020-12-15 11:43:19 -06:00
|
|
|
ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(¯o_rules))),
|
2020-03-10 09:01:08 -05:00
|
|
|
kind: MacroDefKind::BuiltInEager(expander),
|
2020-04-30 22:23:03 -05:00
|
|
|
local_inner: false,
|
2020-03-10 09:01:08 -05:00
|
|
|
};
|
2019-11-22 11:47:35 -06:00
|
|
|
|
2020-12-15 11:43:19 -06:00
|
|
|
let args = macro_call.token_tree().unwrap();
|
2020-03-10 09:01:08 -05:00
|
|
|
let parsed_args = mbe::ast_to_token_tree(&args).unwrap().0;
|
|
|
|
|
|
|
|
let arg_id = db.intern_eager_expansion({
|
|
|
|
EagerCallLoc {
|
|
|
|
def,
|
|
|
|
fragment: FragmentKind::Expr,
|
|
|
|
subtree: Arc::new(parsed_args.clone()),
|
2020-06-11 05:08:24 -05:00
|
|
|
krate,
|
2020-03-10 09:01:08 -05:00
|
|
|
file_id: file_id.into(),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-11-30 12:21:25 -06:00
|
|
|
let (subtree, fragment) = expander.expand(&db, arg_id, &parsed_args).value.unwrap();
|
2020-03-10 09:01:08 -05:00
|
|
|
let eager = EagerCallLoc {
|
|
|
|
def,
|
|
|
|
fragment,
|
|
|
|
subtree: Arc::new(subtree),
|
2020-06-11 05:08:24 -05:00
|
|
|
krate,
|
2020-03-10 09:01:08 -05:00
|
|
|
file_id: file_id.into(),
|
|
|
|
};
|
|
|
|
|
2020-04-19 14:15:49 -05:00
|
|
|
let id: MacroCallId = db.intern_eager_expansion(eager).into();
|
2020-03-10 09:01:08 -05:00
|
|
|
id.as_file()
|
|
|
|
}
|
|
|
|
};
|
2019-11-22 11:47:35 -06:00
|
|
|
|
2020-03-10 09:01:08 -05:00
|
|
|
db.parse_or_expand(file_id).unwrap().to_string()
|
2019-11-22 11:47:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_column_expand() {
|
|
|
|
let expanded = expand_builtin_macro(
|
|
|
|
r#"
|
2019-12-21 06:33:44 -06:00
|
|
|
#[rustc_builtin_macro]
|
|
|
|
macro_rules! column {() => {}}
|
|
|
|
column!()
|
|
|
|
"#,
|
2019-11-22 11:47:35 -06:00
|
|
|
);
|
|
|
|
|
2020-01-14 11:47:01 -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#"
|
2019-12-21 06:33:44 -06:00
|
|
|
#[rustc_builtin_macro]
|
|
|
|
macro_rules! line {() => {}}
|
|
|
|
line!()
|
|
|
|
"#,
|
2019-11-22 11:47:35 -06:00
|
|
|
);
|
|
|
|
|
2020-01-14 11:47:01 -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#"
|
2019-12-21 06:33:44 -06:00
|
|
|
#[rustc_builtin_macro]
|
|
|
|
macro_rules! stringify {() => {}}
|
|
|
|
stringify!(a b c)
|
|
|
|
"#,
|
2019-11-22 11:47:35 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(expanded, "\"a b c\"");
|
|
|
|
}
|
|
|
|
|
2019-12-21 06:33:44 -06:00
|
|
|
#[test]
|
|
|
|
fn test_env_expand() {
|
|
|
|
let expanded = expand_builtin_macro(
|
|
|
|
r#"
|
|
|
|
#[rustc_builtin_macro]
|
|
|
|
macro_rules! env {() => {}}
|
|
|
|
env!("TEST_ENV_VAR")
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
2020-05-12 16:51:38 -05:00
|
|
|
assert_eq!(expanded, "\"__RA_UNIMPLEMENTED__\"");
|
2019-12-21 06:33:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_option_env_expand() {
|
|
|
|
let expanded = expand_builtin_macro(
|
|
|
|
r#"
|
|
|
|
#[rustc_builtin_macro]
|
|
|
|
macro_rules! option_env {() => {}}
|
|
|
|
option_env!("TEST_ENV_VAR")
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
2020-03-04 10:12:39 -06:00
|
|
|
assert_eq!(expanded, "std::option::Option::None:: < &str>");
|
2019-12-21 06:33:44 -06:00
|
|
|
}
|
|
|
|
|
2019-11-22 11:47:35 -06:00
|
|
|
#[test]
|
|
|
|
fn test_file_expand() {
|
|
|
|
let expanded = expand_builtin_macro(
|
|
|
|
r#"
|
2019-12-21 06:33:44 -06:00
|
|
|
#[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
|
|
|
|
2020-03-11 10:08:12 -05:00
|
|
|
#[test]
|
|
|
|
fn test_assert_expand() {
|
|
|
|
let expanded = expand_builtin_macro(
|
|
|
|
r#"
|
|
|
|
#[rustc_builtin_macro]
|
|
|
|
macro_rules! assert {
|
2020-03-11 12:57:34 -05:00
|
|
|
($cond:expr) => ({ /* compiler built-in */ });
|
|
|
|
($cond:expr, $($args:tt)*) => ({ /* compiler built-in */ })
|
2020-03-11 10:08:12 -05:00
|
|
|
}
|
|
|
|
assert!(true, "{} {:?}", arg1(a, b, c), arg2);
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(expanded, "{{(&(true), &(\"{} {:?}\"), &(arg1(a,b,c)), &(arg2),);}}");
|
|
|
|
}
|
|
|
|
|
2019-11-24 18:01:51 -06:00
|
|
|
#[test]
|
|
|
|
fn test_compile_error_expand() {
|
|
|
|
let expanded = expand_builtin_macro(
|
|
|
|
r#"
|
2019-12-21 06:33:44 -06:00
|
|
|
#[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
|
|
|
);
|
|
|
|
|
2020-12-03 08:31:04 -06:00
|
|
|
// This expands to nothing (since it's in item position), but emits an error.
|
|
|
|
assert_eq!(expanded, "");
|
2019-11-24 18:01:51 -06:00
|
|
|
}
|
2019-12-06 12:30:01 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_args_expand() {
|
|
|
|
let expanded = expand_builtin_macro(
|
|
|
|
r#"
|
2019-12-21 06:33:44 -06:00
|
|
|
#[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,
|
2020-03-04 10:12:39 -06:00
|
|
|
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-12 07:34:03 -06:00
|
|
|
);
|
2019-12-06 12:30:01 -06:00
|
|
|
}
|
2020-06-27 13:02:47 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_include_bytes_expand() {
|
|
|
|
let expanded = expand_builtin_macro(
|
|
|
|
r#"
|
|
|
|
#[rustc_builtin_macro]
|
|
|
|
macro_rules! include_bytes {
|
|
|
|
($file:expr) => {{ /* compiler built-in */ }};
|
|
|
|
($file:expr,) => {{ /* compiler built-in */ }};
|
|
|
|
}
|
|
|
|
include_bytes("foo");
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(expanded, r#"b"""#);
|
|
|
|
}
|
2020-12-08 13:06:41 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_concat_expand() {
|
|
|
|
let expanded = expand_builtin_macro(
|
|
|
|
r##"
|
|
|
|
#[rustc_builtin_macro]
|
|
|
|
macro_rules! concat {}
|
|
|
|
concat!("foo", 0, r#"bar"#);
|
|
|
|
"##,
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(expanded, r#""foo0bar""#);
|
|
|
|
|
|
|
|
// FIXME: `true`/`false` literals don't work.
|
|
|
|
}
|
2019-11-22 11:47:35 -06:00
|
|
|
}
|